C++ Double Address Operator? (&&)

C++StlOperator KeywordMemory Address

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

&& 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:

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.

Solution 2 - C++

This is C++11 code. In C++11, the && token can be used to mean an "rvalue reference".

Solution 3 - C++

&& 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.

Solution 4 - C++

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.

Solution 5 - C++

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.

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=()!

MSDN -- How to create a move constructor

Solution 6 - C++

I believe && is for move semantics. It allows something to give up its memory to something else that needs it, avoiding the need for copying.

Move constructor E.g.

String(String&& other) noexcept
{
//...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnarkiView Question on Stackoverflow
Solution 1 - C++Lexseal LinView Answer on Stackoverflow
Solution 2 - C++ascheplerView Answer on Stackoverflow
Solution 3 - C++Billy ONealView Answer on Stackoverflow
Solution 4 - C++Michael BurrView Answer on Stackoverflow
Solution 5 - C++yash101View Answer on Stackoverflow
Solution 6 - C++Jonh Smith BenjaminView Answer on Stackoverflow