Tuesday, September 19, 2017

Digest for comp.lang.c++@googlegroups.com - 7 updates in 2 topics

"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 19 08:35PM +0200

my answer in stl/ref thread didn't show up, so, testing (sorry).
alf.p.steinbach@gmail.com: Sep 19 11:45AM -0700

On Tuesday, September 19, 2017 at 8:35:53 PM UTC+2, Alf P. Steinbach wrote:
> my answer in stl/ref thread didn't show up, so, testing (sorry).
 
Why am I posting to clc++ instead of e.g. no.alt.test? Because the problem is with clc++, when posting from Thunderbird. Again, sorry for the noise.
scott@slp53.sl.home (Scott Lurndal): Sep 19 07:54PM

>my answer in stl/ref thread didn't show up, so, testing (sorry).
 
propagation delay is inherent in Usenet.
porparek@gmail.com: Sep 19 08:56AM -0700

Hi,
 
Could you please explain me why references cannot be stored in STL containers.
I found that one of the reasons is that reference cannot be re-initialized.
If this is true then why the "T * const" may be stored in STL containers ?
Please write me why references cannot be stored in STL containers.
 
thanks for help
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 19 06:47PM +0200


> Could you please explain me why references cannot be stored in STL containers.
 
They can't be stored DIRECTLY in containers.
 
Most containers, and in particular those in the standard library, don't
support reference element type because it would need extra machinery and
interface semantics to handle the not-object-ness of references, e.g.
that you can't have a pointer to a reference, which might be needed
internally in the container's code.
 
 
> I found that one of the reasons is that reference cannot be re-initialized.
> If this is true then why the "T * const" may be stored in STL containers ?
 
A pointer is copyable value, and when stored it's an object.
 
• • •
 
Let's say that you want a vector of references to objects of the same type.
 
Since you can't do that, simply store raw pointers:
 
 
#include <iostream>
#include <vector>
using namespace std;
 
auto main()
-> int
{
int a = 100, b = 200, c = 300;
 
vector<int*> values;
for( auto const p : {&a, &b, &c} ) { values.push_back( p ); }
 
++a; ++b; ++c;
for( int const* const p : values ) { cout << *p << " "; }
cout << endl;
}
 
 
Output:
 
 
 
101 201 301
 
 
Cheers & hth.,
 
- Alf
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Sep 19 06:19PM +0100

> If this is true then why the "T * const" may be stored in STL containers ?
> Please write me why references cannot be stored in STL containers.
 
> thanks for help
 
You won't get very far trying to use std::vector with "T* const" or any
other const value_type.
 
/Flibble
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 19 08:34PM +0200


> Could you please explain me why references cannot be stored in STL containers.
 
They can't be stored DIRECTLY in containers.
 
Most containers, and in particular those in the standard library, don't
support reference element type because it would need extra machinery and
interface semantics to handle the not-object-ness of references, e.g.
that you can't have a pointer to a reference, which might be needed
internally in the container's code.
 
 
> I found that one of the reasons is that reference cannot be re-initialized.
> If this is true then why the "T * const" may be stored in STL containers ?
 
A pointer is copyable value, and when stored it's an object.
 
• • •
 
Let's say that you want a vector of references to objects of the same type.
 
Since you can't do that, simply store raw pointers:
 
 
#include <iostream>
#include <vector>
using namespace std;
 
auto main()
-> int
{
int a = 100, b = 200, c = 300;
 
vector<int*> values;
for( auto const p : {&a, &b, &c} ) { values.push_back( p ); }
 
++a; ++b; ++c;
for( int const* const p : values ) { cout << *p << " "; }
cout << endl;
}
 
 
Output:
 
 
 
101 201 301
 
 
Cheers & hth.,
 
- Alf
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: