Reputation: 16300
Should this code, compiled with -fno-elide-constructors
print "cctor"?
#include<iostream>
struct non_auto
{
non_auto()=default;
private:
non_auto(const non_auto&)
{std::cout << "cctor" << std::endl;}
};
non_auto foo()
{
return non_auto{};
}
int main()
{
auto z = foo();
}
It turns out, for all the versions of GCC I tried, it didn't print anything and effectively elided the constructor.
https://godbolt.org/z/rPTbzzza8
What am I missing? Is it that elision can happen anyway, even with this option?
UPDATE: I am using C++17, it seems that in C++14 works but it is not very interesting because I was trying this flag to reproduce the behavior of C++14 in C++17.
Upvotes: 0
Views: 196
Reputation: 17063
Going by the gcc documentation:
In C++17, the compiler is required to omit these temporaries
When compiling for C++17 or later, this flag is largely ineffective as far as RVO (returning a temporary object) goes because the language mandates that the temporaries be omitted. There is nothing to copy, so there is no copy to keep around (or to elide away; there is no option anymore).
Note: In contrast, this flag does still affect NRVO (returning a variable). However, in the case of non_auto
, this would have to be done in a (possibly static) member function, since the copy constructor is private.
Upvotes: 1