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

Started by
8 comments, last by DerekSaw 15 years, 10 months ago
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.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
Advertisement
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.
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.
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.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
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.
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.
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.
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.
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.
Use pointers when we need to make decision from NULL-ness.

Use references when we want the caller to be responsible for passing valid parameters.
"after many years of singularity, i'm still searching on the event horizon"

This topic is closed to new replies.

Advertisement