Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

references in stl

64 views
Skip to first unread message

porp...@gmail.com

unread,
Sep 19, 2017, 11:56:43 AM9/19/17
to
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

unread,
Sep 19, 2017, 12:47:29 PM9/19/17
to
On 9/19/2017 5:56 PM, porp...@gmail.com wrote:
>
> 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

unread,
Sep 19, 2017, 1:19:56 PM9/19/17
to
You won't get very far trying to use std::vector with "T* const" or any
other const value_type.

/Flibble

Alf P. Steinbach

unread,
Sep 19, 2017, 2:35:18 PM9/19/17
to
On 9/19/2017 5:56 PM, porp...@gmail.com wrote:
>
> 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 ?

Ryan Bird

unread,
Sep 20, 2017, 12:27:51 AM9/20/17
to
References are not reassignable. However, std::reference_wrapper was made to allow reassignable references.

http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
http://www.cplusplus.com/reference/functional/reference_wrapper/

Alf P. Steinbach

unread,
Sep 20, 2017, 2:54:25 AM9/20/17
to
Oh, I didn't notice that `const`. :( You're very right about that.

The relevant error message from Visual C++ is very informative:

“error C2338: The C++ Standard forbids containers of const elements
because allocator<const T> is ill-formed.”

E.g. this pair of overloads in `std::allocator`,

pointer address( reference x ) const;
const_pointer address( const_reference x ) const;

would have the same signature when `reference` is defined as e.g. `int
const&`. And one can't define overloads with the same signature. That's
the “ill-formed” in the Visual C++ error message.


Cheers!,

- Alf

porp...@gmail.com

unread,
Sep 20, 2017, 12:16:35 PM9/20/17
to
Great thanks for reply.
On gcc-7.2.0 the following code compiles and runs with no errors:
std::list<int*const> l;
l.push_back(new int(99));
for ( int* i : l )
std::cout << *i;

Mr Flibble

unread,
Sep 20, 2017, 1:51:53 PM9/20/17
to
Just because it works for a subset of operations on a subset of
container types doesn't mean it works for a different subset of
operations on a different subset of container types.

/Flibble
0 new messages