Old problem with performance

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Feb 12 19:56:55 PST 2009


Rainer Deyke wrote:
> Daniel Keep wrote:
>> It's not a bug.  There are differences between value types and reference
>> types.  Just like how there are differences between atomic types and
>> aggregate types.  Or constant types and mutable types.
> 
> This is a bug:
> 
> struct MathematicalVector {
>   this(int size) {
>     this.values = new T[size];
>   }
>   // No copy constructor.
>   // Insert standard mathematical operators here.
>   private T[] values; // Implementation detail.
> }
> 
> I want MathematicalVector to be a value type (which is why I declared it
> as a struct).  However, it doesn't behave a value type because I forgot
> to write the copy constructor.  D doesn't cause the bug, but it
> certainly makes it easier to accidentally write this kind of bug.
> 
> By contrast, the compiler-generated copy constructors in C++ usually do
> the right thing for all but a handful of low-level resource-management
> classes.  Which is not to say that C++ doesn't have problems of its own
> - clearly it does, or I wouldn't be looking at D.

C++ ctors won't do the right thing if you use pointers, which is the 
moral equivalent of using T[] inside MathematicalVector. If you refer to 
std::vector instead, then that's a carefully-defined type that does have 
the right copy constructor defined.

Andrei



More information about the Digitalmars-d mailing list