دنبال کننده ها

۱۳۹۶ اسفند ۲۳, چهارشنبه

c++11 - Compiler Optimisation in the context of copy elision

[ad_1]



My reference is to the code segment presented as part of the follow-up discussion on the below write-up:



Lambdas vs. Closures



The pertinent code segment is



struct PrintD

~PrintD()

cout << "dtor" << endl;

;

int main()

PrintD d;
auto f = [d]()
d;
;

cout << "--" << endl;

return 0;



When the above code is compiled using GCC without the -fno-elide-constructors option, the following output is generated:



--
dtor
dtor


When recompiled using the -fno-elide-constructors option, the following output is generated:



dtor
--
dtor
dtor


Is the understanding correct that in the latter case, the additional dtor print out corresponds to the temporary being destroyed, which is created during the capture of d by copy inside f?




[ad_2]

لینک منبع