const(X) member of Y

Maxim Fomin maxim at maxim-fomin.ru
Wed Feb 6 14:29:48 PST 2013


On Wednesday, 6 February 2013 at 22:00:52 UTC, Dan wrote:
> On Wednesday, 6 February 2013 at 21:52:01 UTC, Maxim Fomin 
> wrote:
>>
>> but this(const this) is supported and means to be a constructor
>
> Maxim, please review this thread: 
> http://forum.dlang.org/post/lrnxsvtwqwqtydggjphs@forum.dlang.org
>
> In this example the 'this(const this)' is not called, just as 
> pointed out in the previous thread. That is, things have not 
> changed.
> So, in following, "Hi" is not printed. Have you had any luck 
> with 'this(const this)'?
>
> Thanks
> Dan
>
> import std.stdio;
>
> struct X {
>   char[] c;
>   this(char[] c_) { c = c_.dup; }
>   this(const this) {
>     writeln("Hi");
>   }
> }
>
> void main() {
>   auto const x = X(['a']);
>   const(X) x2 = x;
>   writeln("Done");
> }

Yes, because this(const this) is not a copy constructor or a 
postblit, it is a simple constructor. X(x) will print "Hi". This 
is unintuitive, but parameter names can be omitted and this leads 
to confusion between struct postblit and struct constructor which 
has first argument of its own type.


More information about the Digitalmars-d mailing list