gpt4 book ai didi

C++ 双地址运算符? (&&)

转载 作者:IT老高 更新时间:2023-10-28 11:53:39 24 4
gpt4 key购买 nike

我正在阅读 STL 源代码,但我不知道 && 地址运算符应该做什么。这是 STL_vector.h 中的代码示例:

vector&
operator=(vector&& __x) // <-- Note double ampersands here
{
// NB: DR 675.
this->clear();
this->swap(__x);
return *this;
}

“地址的地址”有意义吗?为什么它有两个地址运算符而不是一个?

最佳答案

&& 是 C++11 中的新内容。 int&& a 表示“a”是一个右值引用。 && 通常只用于声明函数的参数。它采用右值表达式。如果你不知道什么是 r 值,简单的解释是它没有内存地址。例如。数字 6 和字符“v”都是 r 值。 int a,a 是左值,而 (a+2) 是右值。例如:

void foo(int&& a)
{
//Some magical code...
}

int main()
{
int b;
foo(b); //Error. An rValue reference cannot be pointed to a lValue.
foo(5); //Compiles with no error.
foo(b+3); //Compiles with no error.

int&& c = b; //Error. An rValue reference cannot be pointed to a lValue.
int&& d = 5; //Compiles with no error.
}

希望能提供信息。

关于C++ 双地址运算符? (&&),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4549151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com