测试函数返回一个值而不使用复制/移动构造函数,复制/移动赋值运算符或复制省略(在g ++ 4.8.4中通过-fno-elide-constructors禁用)
struct Test
{
Test() {}
Test(const Test&) = delete;
Test(Test&&) = delete;
Test& operator = (const Test&) = delete;
Test& operator = (Test&&) = delete;
void fn(){}
};
Test test() {
// error: use of deleted function ‘Test::Test(Test&&)’
// return Test{};
// However, this compiles (g++ 4.8.4 and g++ 6.1.0)
return {};
}
int main() {
test().fn();
// In any case
// error: use of deleted function ‘Test::Test(Test&&)’
// Test t{test()};
}
代码是否有效,如果有效,return {}
会产生什么影响?
答案 0 :(得分:4)
[stmt.return] / 1 ...带有 braced-init-list 的return语句初始化要通过副本从函数返回的对象或引用-list-initialization(8.5.4)来自指定的初始化列表...