www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does std.range.zip use emplace instead of simple assignments?

reply David Nadlinger <see klickverbot.at> writes:
It might well be that I'm missing something obvious in the emplace() 
overload jungle, but what is the reason for std.range.zip to use 
std.conv.emplace for assigning the range elements to the »output« 
elements instead of just using the assignment operator?

https://github.com/D-Programming-Language/phobos/blob/master/std/range.d#L3022
https://github.com/D-Programming-Language/phobos/blob/master/std/range.d#L3058
(and a few other instances)

I'm asking because this is breaking zip() for ranges of class instances 
(the »assignment« overload of emplace() is not defined for them), and 
I'm wondering about the best way to fix it.

Thanks,
David
Sep 01 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/01/2011 10:23 AM, David Nadlinger wrote:
 It might well be that I'm missing something obvious in the emplace()
 overload jungle, but what is the reason for std.range.zip to use
 std.conv.emplace for assigning the range elements to the »output«
 elements instead of just using the assignment operator?

 https://github.com/D-Programming-Language/phobos/blob/master/std/range.d#L3022

 https://github.com/D-Programming-Language/phobos/blob/master/std/range.d#L3058

 (and a few other instances)

 I'm asking because this is breaking zip() for ranges of class instances
 (the »assignment« overload of emplace() is not defined for them), and
 I'm wondering about the best way to fix it.

 Thanks,
 David
This is there to get move semantics, for efficiency. It avoids calling the postblit constructor. This does not apply to ranges that are classes, so the solution would be to add a special case for classes.
Sep 01 2011