前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则编译边学-F.45 不要返回右值引用

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

作者头像
面向对象思考
发布2020-03-25 15:32:11
7990
发布2020-03-25 15:32:11
举报

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:

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

代码语言:javascript
复制
auto&& x = max(0, 1);   // OK, so far
foo(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是通过值返回的:

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

Better(较好):

代码语言:javascript
复制
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。

觉得本文有帮助?请分享给更多人。

关注【面向对象思考】,轻松学习每一天!

有任何疑问,欢迎留言提问或讨论。


面向对象设计,面向对象编程,面向对象思考!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • F.45: Don't return a T&&
    • Reason(原因)
      • Example(示例)
        • Example(示例)
          • Exception(例外)
            • Enforcement(实施建议)
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档