On Tue, 01 Feb 2011 14:26:26 -0500, Simon Buerger <k...@gmx.net> wrote:
On 01.02.2011 18:08, Steven Schveighoffer wrote:
swap isn't the problem.
foreach(s; myVectorSet)
{
// if s is by value, it must be copied for each iteration in the loop
}
Just to note: the "correct" solution for the last problem is
foreach(const ref s; myVectorSet)
which is working in current D. In a more value-based language you may
even want to default to "const ref" for foreach-loop-values, and even
for function-parameters. I suggested that a while ago, but wasn't liked
much for D, for good reasons.
This is not a "solution". I cannot enforce that someone uses foreach with
ref semantics. Unless the type is a reference type, like a class.
Note that const ref isn't necessarily want, you could want to mutate the
elements of the vector, meaning you really want ref.
The whole point is, it's too easy to get it wrong, and the wrong thing
looks innocuous and does not look like it will perform horribly.
Compare this to someone who actually *wants* value semantics when
iterating a reference type (which should be very rare):
foreach(s; myVectorSet)
{
auto setIAmGoingToUse = s.dup; // clear intentions: "yes, I want to do
an expensive copy"
}
-Steve