C++ 自动&与自动 [英] C++ auto& vs auto

查看:29
本文介绍了C++ 自动&与自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建局部变量时,使用(const) auto&还是auto正确?

When creating local variables, is it correct to use (const) auto& or auto?

例如:

SomeClass object;
const auto result = object.SomeMethod();

const auto&result = object.SomeMethod();

其中 SomeMethod() 返回一个非原始值 - 可能是另一个用户定义的类型.我的理解是 const auto&result 是正确的,因为 SomeMethod() 返回的结果将调用返回类型的复制构造函数.如果我错了,请纠正我.

Where SomeMethod() returns a non-primitive value - maybe another user-defined type. My understanding is that const auto& result is correct since the result returned by SomeMethod() would call the copy constructor for the returned type. Please correct me if I am wrong.

原始类型呢?我假设 const auto sum = 1 + 2; 是正确的.

What about for primitive types? I assume const auto sum = 1 + 2; is correct.

这是否也适用于基于范围的 for 循环?

Does this also apply to range based for loops?

for(const auto& object : objects)

推荐答案

autoauto && 涵盖了大部分情况:

auto and auto && cover most of the cases:

  • 当您需要本地副本时使用 auto.这永远不会产生参考.复制(或移动)构造函数必须存在,但由于复制省略优化,它可能不会被调用.

  • Use auto when you need a local copy. This will never produce a reference. The copy (or move) constructor must exist, but it might not get called, due to the copy elision optimization.

当您不关心对象是否为本地对象时,请使用 auto &&.从技术上讲,这将始终产生一个引用,但如果初始化程序是临时的(例如,函数按值返回),它的行为本质上就像您自己的本地对象.

Use auto && when you don't care if the object is local or not. Technically, this will always produce a reference, but if the initializer is a temporary (e.g., the function returns by value), it will behave essentially like your own local object.

此外,auto && 也不保证该对象是可修改的.给定一个 const 对象或引用,它会推导出 const.但是,鉴于特定的上下文,通常会假设可修改性.

Also, auto && doesn't guarantee that the object will be modifiable, either. Given a const object or reference, it will deduce const. However, modifiability is often assumed, given the specific context.

auto &auto const & 更具体一点:

  • auto & 保证您正在与其他东西共享变量.它始终是一个参考,而不是临时的.

  • auto & guarantees that you are sharing the variable with something else. It is always a reference and never to a temporary.

auto const & 类似于 auto &&,但提供只读访问.

auto const & is like auto &&, but provides read-only access.

原始/非原始类型呢?

没有区别.

这是否也适用于基于范围的 for 循环?

Does this also apply to range based for loops?

是的.应用上述原则,

  • 使用 auto && 来修改和丢弃循环内序列的值.(也就是说,除非容器提供只读视图,例如 std::initializer_list,在这种情况下,它实际上是一个 auto const &.)
  • 使用 auto & 以有意义的方式修改序列的值.
  • 使用 auto const & 进行只读访问.
  • 使用 auto 处理(可修改的)副本.
  • Use auto && for the ability to modify and discard values of the sequence within the loop. (That is, unless the container provides a read-only view, such as std::initializer_list, in which case it will be effectively an auto const &.)
  • Use auto & to modify the values of the sequence in a meaningful way.
  • Use auto const & for read-only access.
  • Use auto to work with (modifiable) copies.

您还提到了没有参考的 auto const.这是可行的,但它并不常用,因为对您已经拥有的东西进行只读访问很少有优势.

You also mention auto const with no reference. This works, but it's not very commonly used because there is seldom an advantage to read-only access to something that you already own.

这篇关于C++ 自动&与自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆