improvement request - enabling by-value-containers
Jonathan Schmidt-Dominé
devel at the-user.org
Tue Dec 14 17:50:51 PST 2010
Hi!
>> *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?
Probably, and with param_type bla it is not shorter, but at least it _is_
possible to write generic sort, copy_if etc. without unnecessary copies or
references.
> 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-type in a by-value-type induces a lot of overhead,
wrapping by-value-types in by-reference-types does not. And structs without
default-constructors are not very nice, bacause you would have to check
first in any operation if the wrapped by-reference-container is null.
> copy_if on a multidimensional container should not naively copy entire
> hyperplanes. More generally, I think that whenever an arbitrarily large
> object is to be copied, that should be explicit instead of implicit. A
> lot of focus in C++ is dedicated to making sure you don't copy the wrong
> thing.
Why should it be explicit?
The result of copy_if should be a copy and not a view on the original data,
and even such a view should not rely on the input-types to be by-reference
(by-value types should also be accessible by reference in such a view,
unless it is const, but that is offtopic). When functions like copy_if, map
or fold should be combined in a generic way, by-value-containers are much
more intuitive and they work without guesses where to add a clone. And Array
x(5, [0,1,2]) should result in a 5·3 array.
> It would be great to have D just obviate the necessity of
> parameter_type<T>::result in the first place, which is what auto ref is
> meant for (it's §).
For me something like ref const scope (or ref scope for primitives) should
be used as often as possible, it should even be the default in many cases
(function parameters, foreach-variables etc.), although I understand that
this is unlikely because of compatibility. Then by-value container would
simply make the world consistent and the places where you would have to
write ref or & would be less obtrusive and more generic than all the clone-
stuff you would have to write with by-reference-containers.
> s/The/A/ I guess ;o).
“User” has no specific meaning, it would even be my name, if I would not use
anything in this world. :D
The User
More information about the Digitalmars-d
mailing list