auto&&; 是什么意思?做?

发布于 2025-01-03 03:09:01 字数 906 浏览 0 评论 0原文

这是 Scott Meyers 的 C++11 Notes Sample 中的代码,

int x;
auto&& a1 = x;             // x is lvalue, so type of a1 is int&
auto&& a2 = std::move(x);  // std::move(x) is rvalue, so type of a2 is int&&

我无法理解 auto&&
我对 auto 有一些了解,从中我可以说 auto& a1 = x 应该使 a1 的类型为 int&

从引用的代码来看,这似乎是错误的。

我写了这个小代码,并在 gcc 下运行。

#include <iostream>

using namespace std;

int main()
{
    int x = 4;
    auto& a1 = x;           //line 8
    cout << a1 << endl;
    ++a1;
    cout << x;
    return 0;
}

输出 = 4(换行符)5
然后我将第 8 行修改为 auto&& a1 = x;,然后运行。相同的输出。

我的问题:auto& 等于 auto&& 吗?
如果它们不同,auto&& 会做什么?

This is the code from C++11 Notes Sample by Scott Meyers,

int x;
auto&& a1 = x;             // x is lvalue, so type of a1 is int&
auto&& a2 = std::move(x);  // std::move(x) is rvalue, so type of a2 is int&&

I am having trouble understanding auto&&.
I have some understanding of auto, from which I would say that auto& a1 = x should make type of a1 as int&

Which from Quoted code, seems wrong.

I wrote this small code, and ran under gcc.

#include <iostream>

using namespace std;

int main()
{
    int x = 4;
    auto& a1 = x;           //line 8
    cout << a1 << endl;
    ++a1;
    cout << x;
    return 0;
}

Output = 4 (newline) 5
Then I modified line 8 as auto&& a1 = x;, and ran. Same output.

My question : Is auto& equal to auto&&?
If they are different what does auto&& do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2025-01-10 03:09:01

代码是对的。 自动&& p = expr 表示 p 的类型为 T&&,其中 T 将从 expr 推断出来代码>.这里的&&表示右值引用,因此eg

auto&& p = 1;

会推断T == int,因此p的类型是int&&.

但是,引用可以根据以下规则折叠:(

T& &   == T&
T& &&  == T&
T&& &  == T&
T&& && == T&&

此功能用于在 C++11 中实现完美转发。)

情况下

auto&& p = x;

x 为左值的 ,不能将右值引用绑定到它,但如果我们推断 T = int& 那么 p 的类型将变成 int& && = int&,这是一个左值引用,可以绑定到x。仅在这种情况下,auto&&auto& 给出相同的结果。但这两者是不同的,例如,这

auto& p = std::move(x);

是不正确的,因为 std::move(x) 是右值,并且左值引用无法绑定到它。

请阅读C++ 右值引用解释进行演练。

The code is right. auto&& p = expr means the type of p is T&& where T will be inferred from expr. The && here indicates a rvalue reference, so e.g.

auto&& p = 1;

will infer T == int and thus the type of p is int&&.

However, references can be collapsed according to the rule:

T& &   == T&
T& &&  == T&
T&& &  == T&
T&& && == T&&

(This feature is used to implement perfect forwarding in C++11.)

In the case

auto&& p = x;

as x is an lvalue, an rvalue reference cannot be bound to it, but if we infer T = int& then the type of p will become int& && = int&, which is an lvalue reference, which can be bound to x. Only in this case auto&& and auto& give the same result. These two are different though, e.g.

auto& p = std::move(x);

is incorrect because std::move(x) is an rvalue, and the lvalue reference cannot be bound to it.

Please read C++ Rvalue References Explained for a walk through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文