cannot call impure function ~this
Namespace
rswhite4 at googlemail.com
Wed Oct 16 00:27:23 PDT 2013
On Wednesday, 16 October 2013 at 07:23:45 UTC, monarch_dodra
wrote:
> On Tuesday, 15 October 2013 at 21:37:40 UTC, Namespace wrote:
>> I get this error:
>> ----
>> /d701/f223.d(11): Error: pure function 'f223.getA' cannot call
>> impure function 'f223.A.~this'
>> ----
>>
>> with this code:
>> ----
>> import std.stdio;
>>
>> struct A {
>> public:
>> ~this() {
>> writeln("DTor");
>> }
>> }
>>
>> A getA() pure nothrow {
>> return A();
>> }
>>
>> void main()
>> {
>> A a = getA();
>>
>> writeln("end of main");
>> }
>> ----
>>
>> But without pure and nothrow I get this output:
>> ----
>> end of main
>> DTor
>> ----
>>
>> Why the compiler thinks that the function should/could call
>> A::~this?
>
> It could have something to do with the fact that RVO is an
> optimization *opportunity* that the compiler is allowed to go
> for, even if it changes the program output.
>
> Hoewever, being an *opportunity*, the compiler still has to
> make sure the code is valid without said optimization, which in
> this case, isn't: getA would destroy it's temporary after
> blitting it on the stac, leading to an impure call.
So it _could_ be impure, but mostly it isn't, right?
More information about the Digitalmars-d-learn
mailing list