Postblit not invokable with MyStruct(MyStruct()); ?

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 3 04:38:42 PDT 2014


On 05/03/14 01:05, Mark Isaacson via Digitalmars-d-learn wrote:
> 2) I ran into this issue while attempting to leverage the postblit for code-reuse. In particular, I have a setup that is similar to:
> 
> struct A {
>   this(B b) { /* Stuff */ }
> }
> 
> struct B {
> }
> 
> void foo(T)(T param) {
>   auto a = A(param);
>   /* Stuff */
> }
> 
> unittest {
>   foo(A()); //Fails
>   foo(B()); //Succeeds
> }
> 
> The notion being that A and B are 2 ways to represent the same thing, why not convert everything to the A format and proceed from there; I figured the compiler would optimize out the pointless copy when T == A. Alas, as shown in my unittest, foo fails to accept arguments of type A.

What actually fails is the initialization of 'a'.
Add another

   this(A a) { /* Stuff */ }

constructor to the 'A' struct, and it will work.


And, yes, the missing cpctors are a language problem.

artur


More information about the Digitalmars-d-learn mailing list