带有不可复制类型的链式复制省略

时间:2016-02-17 17:12:34

标签: c++ c++11 gcc clang copy-elision

考虑以下示例:

#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);   
}

我有一个非可复制对象的两个链式复制 - elidable返回。 gcc 5.2编译得很好。但是由于实际上试图执行复制,gcc 4.8和clang 3.7都不喜欢它。哪个编译器是对的?我假设gcc故意改变以允许这种行为,这对于像这样的情况似乎特别有用。

0 个答案:

没有答案