'Johannes Schaub' via ISO C++ Standard - Discussion
2016-08-06 08:42:04 UTC
During a stackoverflow question at
http://stackoverflow.com/questions/38801955/how-does-guaranteed-copy-elision-work-in-list-initialization-in-c1z,
I was of the opinion that copy elision of a list initialization by a
copy constructor of a non-aggregate class type should not be possible
by current C++14 rules. But both GCC and Clang elide the copy
constructor in this example:
#include <iostream>
struct A {
A() { }
A(const A& ) {
std::cout << "not elided\n";
}
};
A f() { return A(); }
A g() { return {A()}; }
int main() {
f();
std::cout << "now g\n";
g();
}
None print "not elided" for the call to g(). Why is that? The elision
paragraph says
"when a temporary class object that has not been bound to a
reference (12.2) would be copied/moved to a class object with the same
cv-unqualified type, the copy/move operation can be omitted by
constructing the temporary object directly into the target of the
omitted copy/move"
This means that the copy of "A()" into a by-value parameter of a
constructor of "A" can be elided.. but not the call to the constructor
of "A" itself!? So are GCC and Clang conforming here?
http://stackoverflow.com/questions/38801955/how-does-guaranteed-copy-elision-work-in-list-initialization-in-c1z,
I was of the opinion that copy elision of a list initialization by a
copy constructor of a non-aggregate class type should not be possible
by current C++14 rules. But both GCC and Clang elide the copy
constructor in this example:
#include <iostream>
struct A {
A() { }
A(const A& ) {
std::cout << "not elided\n";
}
};
A f() { return A(); }
A g() { return {A()}; }
int main() {
f();
std::cout << "now g\n";
g();
}
None print "not elided" for the call to g(). Why is that? The elision
paragraph says
"when a temporary class object that has not been bound to a
reference (12.2) would be copied/moved to a class object with the same
cv-unqualified type, the copy/move operation can be omitted by
constructing the temporary object directly into the target of the
omitted copy/move"
This means that the copy of "A()" into a by-value parameter of a
constructor of "A" can be elided.. but not the call to the constructor
of "A" itself!? So are GCC and Clang conforming here?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.