【发布时间】:2016-07-14 22:51:28
【问题描述】:
使用 C++ 17,我们将有可能返回不可移动(包括不可复制)类型,例如 std::mutex,通过可以被认为是保证返回值优化 (RVO):Guaranteed copy elision through simplified value categories:
struct nocopy { nocopy(nocopy&) = delete; nocopy() = default; };
auto getRVO(){
return nocopy();
}
我们还将有structured bindings,允许:
tuple<T1,T2,T3> f();
auto [x,y,z] = f();
或者(这里也使用我对template argument deduction for constructors这个特性的理解)
template<typename T1,typename T2,typename T3>
struct many {
T1 a;
T2 b;
T3 c;
};
// (Original questions missed 'many' on the next line. Thanks, T.C.)
auto f(){ return many{string(),5.7, false} };
auto [x,y,z] = f();
但是,这些功能组合起来是为了实现这样的功能吗?
auto get_ensured_rvo_str(){
return std::pair(std::string(),nocopy());
}
auto get_class_and_mutex(){
return many{SomeClass(),std::mutex(),std::string()};
}
int main(){
auto rvoStr = get_ensured_rvo_str().first;
auto [ mtx,sc,str ] = get_class_and_mutex();
}
我的想法是,要让它工作,它需要保证聚合构造函数参数的 RVO 在形成 std::tuple 或 many,但不会被命名为 RVO (NRVO),它具体不包含在P0144R2 建议?
旁注:P0144R2 特别提到支持仅移动类型:
2.6 只移动类型
支持仅移动类型。例如:
struct S { int i; unique_ptr<widget> w; }; S f() { return {0, make_unique<widget>()}; } auto [ my_i, my_w ] = f();
【问题讨论】: