Sign in to follow this  

std::map won't accept any reference as second value

This topic is 3485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am using gcc 4.0.2. This code generates an error, tracing back through the inheritance heirarchy of std::map, stating that it cannot create a reference. std::map<unsigned int, int &> nodes; If I change it to <int, int> there is no problem. I initially tried this with a RenderNode&, but thought that may be the problem, the fact that it also occurs with an int rules that out. here is a link to what I get out of gcc I could just settle for passing by value, however that would mean a slight structural change. Who has any idea for what I can do? Thanks for any help.

Share this post


Link to post
Share on other sites
References cannot be reassigned once they are created, so they cannot be stored in a map (since there would be no way to copy or move them around, or default initialize them). Use pointers or values.

Share this post


Link to post
Share on other sites
It wont accept a reference because its not allowed. A reference has to be bound when it is declared. std::map<> is not prepared to handle that requirement.

Why is it important to use a reference? Just use a pointer, it exactly the same except you lose by-value semantics.

Share this post


Link to post
Share on other sites
Quote:
Well i guess this is the solution, but I recently discovered that I was using pointers in everything, C style, and need practice passing by reference.
In modern C++, you can almost always (if not always) avoid the use of raw 'C-style' pointers in your own code. However, references aren't always the answer (as pointed out above).

Based on the info you've provided, I'd suggest taking a look at either boost::shared_ptr or boost::ptr_map.

Share this post


Link to post
Share on other sites
Quote:
Original post by speciesUnknown
Well i guess this is the solution, but I recently discovered that I was using pointers in everything, C style, and need practice passing by reference.


Using references everywhere they are appropriate is good. However, they are simply not appropriate (or indeed possible) to use in a std::map. There's nothing wrong with using pointers in this case, or if your objects are lightweight enough simple values.

Share this post


Link to post
Share on other sites
Quote:
Original post by speciesUnknown
Well i guess this is the solution, but I recently discovered that I was using pointers in everything, C style, and need practice passing by reference.


Using a reference type is not the same as passing by reference.

Share this post


Link to post
Share on other sites
Highly compressed explanation: A map stores key/value pairs, and references aren't values.

Consider this code.

int i = 5;
int& r = i;

Here, r is just another name for the object known as i. r and i are completely interchangable from here on. From a pure language semantics view, r itself does NOT have a value that "refers to i". r and i ARE the same object.

An implementation may use something like a pointer that points to i and store that as a value in r, or it might optimize the reference away completely. But this is completely implementation defined.

Share this post


Link to post
Share on other sites
My rule of thumb for references is use references wherever you can and it makes sense, and no more. For example, my parameters are always either by reference or by const-reference, except for built-in types and arrays, where I will allow by value. As an internal implementation, I'm okay with using pointers where it makes sense. If I know the object it refers to at construction time and it never changes, then the class will also use a reference internally. I actually prefer this in some cases. For a fair amount of classes, allowing assignment doesn't make sense anyway.

In the case of a map, for the reasons stated above (no need to restate them), it can't be done. It's actually one reason I wish C++ had implemented Java-style references and done away with pointers completely. The fact that both exist and you can't fully get rid of pointers, even by policy, is an annoyance.

Share this post


Link to post
Share on other sites

This topic is 3485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this