On Friday, 17 October 2014 at 00:55:25 UTC, ketmar via Digitalmars-d wrote:
On Fri, 17 Oct 2014 00:42:24 +0000
IgorStepanov via Digitalmars-d <[email protected]> wrote:

Can someone comment this code? Should I think that it's a bug.
it's just an anomaly. const postblit can do alot of things besides adjusting struct fields, and it's logical that compiler cannot call
non-const methods for const objects.

yet it's still on of those "unforseen consequences" that arises from
conjunction of different features.

i don't think that it's a bug, but i think that this must be discussed
anyway, and then documented.

I think, this is unexpected behaviour:

Compiler generates "__fieldPostBlit" for all structs, and call it when postblit is needed.

In this example compiler generates something like the next code:

struct A
{
    this(this)
    {
    }
}

struct B
{
    A a;

    void __fieldPostBlit()
    {
        a.__postblit(); //a has an explicit postblit
    }
}


struct C
{
    const B b;
    void __fieldPostBlit()
    {
        b.__fieldPostBlit(); //Error: b is const
    }
}

void main()
{
    C c;
    C c2 = c; //=>
              //memcpy(&c2, &c, C.sizeof)
              //c2.__fieldPostBlit();

}


However, __postblit and __fieldPostBlit are always called for new object, thus it can neglect const guarantee and generate postblits like:
void __fieldPostBlit()
{
     (cast()b).__fieldPostBlit();
}

Reply via email to