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。

 

觉得本文有帮助?欢迎点赞并分享给更多的人。

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值