C ++自动和与汽车 [英] C++ auto& vs auto

查看:74
本文介绍了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.

原始/非原始类型呢?

What about for primitive/non-primitive types?

没有区别.

这是否也适用于基于范围的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天全站免登陆