Copy a struct on the heap and get the pointer to it.

deadalnix deadalnix at gmail.com
Sat Nov 12 08:37:15 PST 2011


Le 12/11/2011 16:27, Andrei Alexandrescu a écrit :
> On 11/12/11 9:22 AM, kenji hara wrote:
>> Here is a pitfall.
>> If S has the postblit, it is not called with your code.
>>
>> import core.stdc.stdio;
>> struct S
>> {
>> this(this){
>> printf("postblit\n");
>> }
>> }
>> void main()
>> {
>> S s;
>> S* ps = [s].ptr;
>> // does not print "postblit"
>> }
>>
>> I've already poted a pull
>> (https://github.com/D-Programming-Language/dmd/pull/375) to fix it,
>> but it is not yet merged.
>
> Thanks! This is important. I recall C++ was much more difficult to use
> in the period of time where compilers got various copy construction
> scenarios wrong. We need to have object copying rock-solid.
>
> Andrei
>
>

Well I don't think the postblit should be called if s isn't used after 
the copy on the heap. This should have the move semantic and not the 
copy semantic.

Obviously, if s is used after the copy on the heap, the postblit should 
be called.


More information about the Digitalmars-d mailing list