Consider the following example:
#include <iostream>
#include <memory>
#include <boost/optional.hpp>
struct X {
boost::optional<std::unique_ptr<int>> foo(int i) {
std::unique_ptr<int> t(new int{i});
return t;
// every compiler works with:
// return std::unique_ptr<int>(new int{i});
}
};
struct Y {
X x;
boost::optional<std::unique_ptr<int>> bar(int i) {
return x.foo(i);
}
};
int main()
{
Y{}.bar(42);
}
I have two chained copy-elidable returns of a non-copyable object. gcc 5.2 compiles it fine. But neither gcc 4.8 nor clang 3.7 like it, due to actually trying to perform the copy. Which compiler is right? I'm assuming gcc intentionally changed to allow this behavior, which seems particularly useful for cases like this.