On 2010-12-14 17:05:47 -0500, Andrei Alexandrescu
<seewebsiteforem...@erdani.org> said:
On 12/14/10 3:29 PM, Jonathan Schmidt-Dominé wrote:
However, some reasons for by-value-containers:
*First of all you often have to deal with mutli-dimensional data-
structures,
you map something to a map of lists of whatever and you want to manage
suche
data in a simple and generic way, simplification or extensions to the data-
structure should not force you to refactor all more or less generic code-
fragments. For example copying some entries around should not look
different
just because you added a dimension in your data-structure. But without
proper value-semantics you are forced to do that, because at some point you
will have to switch from by-value to by-reference because of limitations
made somewhere.
I think this argument goes exactly the other way. C++ containers have
terrible compositional behavior. Using vector<vector<T> > or
vector<map<T> > in C++98 is suicide. C++0x fixes that by means of
introducing rvalue references, but reference semantics obviate all that.
That would depend on what you're doing. Sure, if the next thing you do
is run an algorithm that swaps vectors or maps then will be stupidly
slow. But if you're just filling the containers and iterating on the
data later then it works quite well, and is likely to perform better
than by-reference containers.
Also, won't move semantics fix this whole performance problem?
Reference semantics isn't the only solution.
*Another argument: It should be very simple (at least in C++ it is, I have
never had problems with it, I just added the& here and there) to handle
references to by-value-types, but wrapping by-reference-types into by-value-
types is really ugly, although it may be the right thing somewhere.
"here and there" is more like "every time I define a function". I mean
that's a lot, no?
I agree writing 'const T &' everywhere in C++ is a pain, and it
shouldn't be that way.
Perhaps what we need is a way to tell the compiler that a certain type
should automatically be passed by reference when given as a function
argument. By passing them by reference as in 'ref', you know the
reference won't escape the function's scope, and your container can
even be located on the stack.
Wrapping could work either way, and after thinking about it a lot I
have difficulty decreeing one is considerably easier/simpler than the
other.
Wrapping a by-reference inside a by-value container is easy, but
wasteful (extra allocation, extra dereference, extra null pointer
check). I think that was the point.
--
Michel Fortin
michel.for...@michelf.com
http://michelf.com/