views:

165

answers:

1

In my program I have a STL set.

set<string> myStrings;

To improve efficiency I changed it to only hold pointers. (I don't need actual string copies to be stored.)

set<string*> myStrings;

I read that it is a good practice to substitute pointers with references when possible. (Of course only if the actual functionality of a pointer is not needed.)

set<string&> myStrings;

The latter one gives ma lot of compiler errors, though. Why is it not possible to use references as container elements?

+8  A: 

Containers store objects. References are not objects.

The value type of a container must be both copy constructible and assignable. A reference is not assignable.

James McNellis
mojuba
@mojuba -- In C++, when we say objects, we mean instances of any data type, including builtins.
PigBen
@mojuba: Both `int` and `int*` are objects (in C++, "an object is aregion of storage." (C++03 1.8/1)). A reference is never an object. To quote the C++ standard, "Containers are objects that store other objects" (23.1/1).
James McNellis
@mojuba: A reference is not an object as far as the language is concerned (although it's probably implemented as a pointer under the hood in certain circumstances).
Oli Charlesworth
@mojuba: References are **NOT** pointers. They are aliases and as such are not objects. Everything else you mention is an object and thus is store-able in a standard container. Even thinking of references as an implementation of pointers is simplistic and often not correct (though I suppose it is an easy way to explain references to a C programmers).
Martin York
Ok, but the explanation as to why a ref can't be a container element is not that "it's not an object". Just look at the actual error messages given by the compiler.
mojuba
@mojuba -- read the next two sentences, they give the exact reason.
PigBen
mojuba
@mojuba -- The errors you get in your compiler are dependent upon the specific implementation of the container provided by your compiler. The standard doesn't mandate any particular implementation. The standard takes a very black box approach. It says things like "The value type of a container must be both copy constructible and assignable", without worrying about how the copying and assigning is being accomplished.
PigBen
mojuba
@mojuba: No, that's not true. The compile will fail the first place the particular STL implementation tried to use the assignment operation. That could be 20 function calls deep inside the template implementation code, or 2 function calls deep, and still be all well and good w.r.t. the standard. Oh, and no, references are not assignable.
Billy ONeal
@mojuba: "We are trying to answer a fairly simple question here, aren't we?" Yes, we are, and this answer provides the answer as clearly as I think is possible: containers store objects that must be assignable and references are neither objects nor assignable. There's really nothing more to it. The "double references" issue you bring up is a red herring; C++0x allows for reference collapsing which resolves that issue but still you cannot have a container of references because references are neither objects nor assignable.
James McNellis
@James McNellis: I now agree your answer to the original question is formally correct, but I still don't think it is the best answer that can be given to someone who (obviously) not as experienced in C++.
mojuba