Implementing SmartPtr - compiler bug?
Szymon Gatner via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Oct 28 01:36:05 PDT 2014
On Monday, 27 October 2014 at 18:42:11 UTC, Marc Schütz wrote:
> On Monday, 27 October 2014 at 16:58:56 UTC, Szymon Gatner wrote:
>> On Monday, 27 October 2014 at 14:04:53 UTC, Marc Schütz wrote:
>>> On Monday, 27 October 2014 at 12:40:17 UTC, Shachar Shemesh
>>> wrote:
>>>> On 27/10/14 11:31, Szymon Gatner wrote:
>>>>
>>>>> Right, sorry. Tho I admit I made assumptions since that was
>>>>> not the full
>>>>> code.
>>>>
>>>> I've opened a bug. It has a fully contained sample (that
>>>> does not, in fact, implement smartptr).
>>>>
>>>> https://issues.dlang.org/show_bug.cgi?id=13661
>>>
>>> Strictly speaking, your opAssign is wrong, because it doesn't
>>> swap source and destination, as it should. This doesn't
>>> matter here, but it would for a smartptr.
>>>
>>> However, that opAssign isn't even called is certainly a bug.
>>
>> Why would opAssign() swap things?
>
> This is necessary to make assignment exception safe (in the
> general case). It's known as the copy-and-swap idiom in C++.
> Here it's described in the D spec:
>
> http://dlang.org/struct#assign-overload
>
> For your example code it's not necessary, because there can
> never be exceptions, but depending on what the smart pointer
> does, it might throw in one of its members' opAssign, which
> could lead to a partially copied object.
Copy-and-swap in C++ as the name implies requires 1) copy first -
a temporary copy-constructed object on created on the stack then
2) trivial/nothrow swap with that temporary. Your suggestion
sounded like source and destination of opAssign() were suppose to
be swapped. In C++ this is sometimes used when move-assigning.
More information about the Digitalmars-d-learn
mailing list