Copy Constructor DIP
RazvanN
razvan.nitu1305 at gmail.com
Thu Jul 12 07:17:49 UTC 2018
> That's my point; so this is a compile error then:
> S b = S(a); // <- explicit construction of copy? ie, explicit
> call to
> stated `@implicit` function
Consider this code:
import std.stdio : writeln;
struct S
{
this(this)
{
writeln("postblit");
}
int a;
this(int a)
{
this.a = a;
}
this(ref S another)
{
writeln("calling this");
}
}
void main()
{
S a;
S b = S(a);
}
In this situation, the constructor is called; if `this(ref S
another)` is commented the code will result in a compilation
error "constructor S.this(int) is not callable using argument
types (S)", so the postblit is NOT called. In my opinion this is
the desired behavior and should be implemented by the copy
constructor also. If you are doing `S b = S(a)` then you are
explicitly trying
to call a constructor; if it doesn't exist, then it's an error;
if you wanted to call the copy constructor then you would have
done `S b = a;`. Using S(a) states the intention of explicitly
calling a constructor while the copy constructor can
only be called implicitly.
More information about the Digitalmars-d
mailing list