C++双地址运算符?(&&)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4549151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Double Address Operator? (&&)
提问by Anarki
I'm reading STL source code and I have no idea what &&
address operator is supposed to do. Here is a code example from stl_vector.h
:
我正在阅读 STL 源代码,但我不知道&&
地址运算符应该做什么。这是来自stl_vector.h
:的代码示例:
vector&
operator=(vector&& __x) // <-- Note double ampersands here
{
// NB: DR 675.
this->clear();
this->swap(__x);
return *this;
}
Does "Address of Address" make any sense? Why does it have two address operators instead of just one?
“地址地址”有意义吗?为什么它有两个地址运算符而不是一个?
采纳答案by aschepler
回答by Lexseal Lin
&&
is new in C++11. int&& a
means "a" is an r-value reference. &&
is normally only used to declare a parameter of a function. And it onlytakes a r-value expression. If you don't know what an r-value is, the simple explanation is that it doesn't have a memory address. E.g. the number 6, and character 'v' are both r-values. int a
, a is an l-value, however (a+2)
is an r-value. For example:
&&
在 C++11 中是新的。int&& a
表示“a”是 r 值参考。&&
通常只用于声明函数的参数。它仅需要一个R值表达式。如果您不知道 r 值是什么,简单的解释是它没有内存地址。例如数字 6 和字符 'v' 都是 r 值。int a
, a 是一个左值,但是(a+2)
是一个 r 值。例如:
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.
}
Hope that is informative.
希望这是有益的。
回答by Billy ONeal
&&
is new in C++11, and it signifies that the function accepts an RValue-Reference-- that is, a reference to an argument that is about to be destroyed.
&&
在 C++11 中是新的,它表示函数接受一个RValue-Reference—— 即对即将被销毁的参数的引用。
回答by Michael Burr
As other answers have mentioned, the &&
token in this context is new to C++0x (the next C++ standard) and represent an "rvalue reference".
正如其他答案所提到的,&&
此上下文中的标记是 C++0x(下一个 C++ 标准)的新标记,表示“右值引用”。
Rvalue references are one of the more important new things in the upcoming standard; they enable support for 'move' semantics on objects and permit perfect forwarding of function calls.
右值引用是即将到来的标准中更重要的新事物之一;它们支持对象上的“移动”语义,并允许函数调用的完美转发。
It's a rather complex topic - one of the best introductions (that's not merely cursory) is an article by Stephan T. Lavavej, "Rvalue References: C++0x Features in VC10, Part 2"
这是一个相当复杂的主题 - 最好的介绍之一(不仅仅是粗略)是 Stephan T. Lavavej 的一篇文章,“Rvalue 参考:VC10 中的 C++0x 特性,第 2 部分”
Note that the article is still quite heavy reading, but well worthwhile. And even though it's on a Microsoft VC++ Blog, all (or nearly all) the information is applicable to any C++0x compiler.
请注意,这篇文章的阅读量仍然很大,但非常值得。即使它在 Microsoft VC++ 博客上,所有(或几乎所有)信息都适用于任何 C++0x 编译器。
回答by yash101
I believe that is is a move operator. operator=
is the assignment operator, say vector x = vector y
. The clear()
function call sounds like as if it is deleting the contents of the vector to prevent a memory leak. The operator returns a pointer to the new vector.
我相信这是一个移动运算符。operator=
是赋值运算符,比如说vector x = vector y
。的clear()
函数调用听起来像处理是否它被删除向量的内容,以防止内存泄漏。运算符返回一个指向新向量的指针。
This way,
这边走,
std::vector<int> a(100, 10);
std::vector<int> b = a;
for(unsigned int i = 0; i < b.size(); i++)
{
std::cout << b[i] << ' ';
}
Even though we gave vector a values, vector b has the values. It's the magic of the operator=()
!
即使我们给了向量 a 值,向量 b 也有值。这就是魔力operator=()
!