0
点赞
收藏
分享

微信扫一扫

C++核心准则边译边学-F.45 不要返回右值引用


F.45: Don't return a ​​T&&​

F.45:不要返回右值引用

Reason(原因)

It's asking to return a reference to a destroyed temporary object. A ​​&&​​ is a magnet for temporary objects.

返回右值引用是在要求返回一个已经销毁的临时对象的引用。&&对于临时对象来说具有吸引力。

译者注:后面这句真没理解。

Example(示例)

A returned rvalue reference goes out of scope at the end of the full expression to which it is returned:

返回的右值引用在所有表达式的最后一个返回时退出了有效范围。

 

auto&& x = max(0, 1);   // OK, so farfoo(x);                 // Undefined behavior

This kind of use is a frequent source of bugs, often incorrectly reported as a compiler bug. An implementer of a function should avoid setting such traps for users.

这种用法是bug的常见原因。经常被错误地报告为编译器错误。函数的实现者应该避免给使用者布置这样的陷阱。

The lifetime safety profile will (when completely implemented) catch such problems.

生命周期安全规则群组会(在完全实现的情况下)捕捉这样的问题。

Example(示例)

Returning an rvalue reference is fine when the reference to the temporary is being passed "downward" to a callee; then, the temporary is guaranteed to outlive the function call (see F.18 and F.19). However, it's not fine when passing such a reference "upward" to a larger caller scope. For passthrough functions that pass in parameters (by ordinary reference or by perfect forwarding) and want to return values, use simple ​​auto​​​ return type deduction (not ​​auto&&​​).

当引用的对象是向下(向内)传递给调用者时返回右值引用时会工作得很好。这种情况下可以保证临时变量的生命周期会长于函数调用(参见F.18和F.19)。然而,当将这个引用向上(向外)传递给更大的调用者空间时是有问题的。对于传递输入参数(通过原始引用或者完美的fowrard处理)而且需要返回值的透过型函数,使用简单的auto类型返回类型推断(不是auto&&)。

Assume that ​​F​​ returns by value:

假设F是通过值返回的:

 

template<class F>auto&& wrapper(F f){    log_call(typeid(f)); // or whatever instrumentation    return f();          // BAD: returns a reference to a temporary}

Better(较好):


template<class F>auto wrapper(F f){    log_call(typeid(f)); // or whatever instrumentation    return f();          // OK}

Exception(例外)

​std::move​​​ and ​​std::forward​​​ do return ​​&&​​​, but they are just casts -- used by convention only in expression contexts where a reference to a temporary object is passed along within the same expression before the temporary is destroyed. We don't know of any other good examples of returning ​​&&​​.

std::move和std::forward也会返回&&,但是按照惯例它们只是用于临时变量被销毁之前,它的引用在同一表达式内传递的情况。我们不知道其他任何返回&&的好例子。

Enforcement(实施建议)

Flag any use of ​​&&​​​ as a return type, except in ​​std::move​​​ and ​​std::forward​​.

标记使用&&做返回值类型的用法,除了std::move和std::forward。

 


阅读更多更新文章,请关注微信公众号【面向对象思考】

举报

相关推荐

0 条评论