Assuming structs are cheap to copy?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 10 08:20:39 PST 2011


On Sun, 09 Jan 2011 11:06:22 -0500, Peter Alexander  
<peter.alexander.au at gmail.com> wrote:

> I remember there was a discussion a little while back about how Phobos  
> would assume that structs are cheap to copy, and would simply pass them  
> by value.
>
> Is this assumption now "the D way"? Should we all just pass structs by  
> value, assuming cheap copy construction?

The discussion was about assumptions that ranges will make on their  
elements.  Essentially, ranges will expect their element types to be  
cheaply copied.

This does not mean that all structs need be cheaply copyable, nor that  
ranges cannot be used with expensive-to-copy element types.  It just means  
that for the algorithms defined on ranges, the complexity stated depends  
on the elements being cheaply copyable.

> If so, I think this needs to be documented somewhere (assuming it isn't  
> already), because it's a radical departure from C++, and also from most  
> other languages (where everything is a reference type). People need to  
> be aware of this.

AFAIK, there has not been a conclusion to this.  Once there is, if cheap  
copy assumption is the result, then I expect documentation to reflect that  
(probably at top of std.range and std.algorithm).

-Steve


More information about the Digitalmars-d mailing list