Arrays of variants, C++ vs D

Ali Çehreli acehreli at yahoo.com
Thu Jun 17 21:01:26 UTC 2021


On 6/17/21 1:46 PM, Steven Schveighoffer wrote:

 > Implicit construction is supported:
 >
 > struct Foo
 > {
 >     int x;
 >     this(int y) { x = y; }
 > }
 >
 > Foo f = 5; // ok implicit construction

That's so unlike the rest of the language that I consider it to be a 
bug. :) Really, why? What if the expression is more complicated:

int i() {
   return 1;
}

   Foo f = i();

OK, that works as well. Wow!

But the following doesn't and is what I think Walter has been trying to 
prevent:

struct Foo
{
    int x;
    this(int y) { x = y; }
}

void foo(Foo) {
}

void main() {
   foo(42);    // COMPILATION ERROR
}

What's the difference? In both cases an int is being converted to a Foo. 
I think the "working" case is against the design of D.

Likely there is a subtlety that I am missing...

Ali



More information about the Digitalmars-d-learn mailing list