c++11中auto&&是什么意思?

By using auto&& var = <initializer> you are saying: I will accept any initializer regardless of whether it is an lvalue or rvalue expression and I will preserve its constness. This is typically used for forwarding (usually with T&&). The reason this works is because a "universal reference", auto&& or T&&, will bind to anything.

You might say, well why not just use a const auto& because that will also bind to anything? The problem with using a const reference is that its const! You wont be able to later bind it to any non-const references or invoke any member functions that are not marked const.

As an example, imagine that you want to get a std::vector, take an iterator to its first element and modify the value pointed to by that iterator in some way:

auto&& vec = some_expression_that_may_be_rvalue_or_lvalue; auto i = std::begin(vec); (*i)++; This code will compile just fine regardless of the initializer expression. The alternatives to auto&& fail in the following ways:

auto => will copy the vector, but we wanted a reference auto& => will only bind to modifiable lvalues const auto& => will bind to anything but make it const, giving us const_iterator const auto&& => will bind only to rvalues So for this, auto&& works perfectly! An example of using auto&& like this is in a range-based for loop.

By using auto&& var = you are saying: I will accept any initializer regardless of whether it is an lvalue or rvalue expression and I will preserve its constness. This is typically used for forwarding (usually with T&&). The reason this works is because a "universal reference", auto&& or T&&, will bind to anything. You might say, well why not just use a const auto& because that will also bind to anything? The problem with using a const reference is that its const! You wont be able to later bind it to any non-const references or invoke any member functions that are not marked const. As an example, imagine that you want to get a std::vector, take an iterator to its first element and modify the value pointed to by that iterator in some way: auto&& vec = some_expression_that_may_be_rvalue_or_lvalue; auto i = std::begin(vec); (*i)++; This code will compile just fine regardless of the initializer expression. The alternatives to auto&& fail in the following ways: auto => will copy the vector, but we wanted a reference auto& => will only bind to modifiable lvalues const auto& => will bind to anything but make it const, giving us const_iterator const auto&& => will bind only to rvalues So for this, auto&& works perfectly! An example of using auto&& like this is in a range-based for loop.
经验分享 程序员 微信小程序 职场和发展