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
:
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?
&&
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.As other answers have mentioned, the
&&
token in this context is new to C++0x (the next C++ standard) and represent an "rvalue reference".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"
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.
This is C++11 code. In C++11, the
&&
token can be used to mean an "rvalue reference".&&
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 only takes 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:Hope that is informative.
I believe that is is a move operator.
operator=
is the assignment operator, sayvector x = vector y
. Theclear()
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.This way,
Even though we gave vector a values, vector b has the values. It's the magic of the
operator=()
!MSDN -- How to create a move constructor