Arrays of variants, C++ vs D

Steven Schveighoffer schveiguy at gmail.com
Thu Jun 17 21:25:10 UTC 2021


On 6/17/21 5:01 PM, Ali Çehreli wrote:

> 
> 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...

The difference might be that construction has only one set of overloads 
-- the constructor of the item being created. With your example, there's 
the set of overloads to create a Foo and the set of overloads to call foo.

Imagine this:

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

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

void foo(Foo f){}
void foo(Bar b){}

void main()
{
    foo(42); // which one?
}

But I don't know. I know that there are all kinds of subtle problems 
with C++ implicit conversions, and D is right to avoid all that. But 
this might be one case where the decision tree is easy.

-Steve


More information about the Digitalmars-d-learn mailing list