我刚刚看了 Scott Meyers Universal References in C++11有一件事我不太明白。
我对作为“通用引用”的 auto
之间的区别感到有点困惑,即 auto&&
和常规 auto
,它们什么时候不同?
Foo f;
Foo& lvr = f;
auto lvr_a = f; // Foo&
auto rvr_a = std::move(f); // Foo&& or is it Foo?
auto&& lvr_b = f; // Foo& && => Foo&
auto&& lvr_b = std::move(f); // Foo&& && => Foo&&
最佳答案
auto 将衰减为值类型(即使用复制构造函数进行复制),而 auto&& 将保留引用类型。
参见此处:C++11: Standard ref for action of `auto` on const and reference types
auto rvr_a = std::move(f); // Foo&& or is it Foo?
它只是 Foo
。所以这与:
Foo rvr_a = std::move(f); // Foo&& or is it Foo?
但请注意,如果有移动构造函数,它仍会调用移动构造函数。
关于c++ - 自动类型推导和 auto&& 与 auto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12818432/