224

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?

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
Anarki
  • 4,983
  • 4
  • 19
  • 9
  • 4
    Maybe it's an address of a reference. – Gabe Dec 28 '10 at 20:15
  • 2
    @Gabe; it's a declaration so that would make it a reference to a reference, which doesn't make any sense as the reference itself can't be modified. The address-of can only be used in the code, not when declaring (a parameter as in this case, or otherwise). Never seen anything like this though. – falstro Dec 28 '10 at 20:17
  • 6
    Even if there was only a single `&`, it would have nothing to do with the address-of operator, but instead signify that `__x` is a reference. – sepp2k Dec 28 '10 at 20:20
  • 3
    Possible duplicate of [What does T&& (double ampersand) mean in C++11?](https://stackoverflow.com/questions/5481539/what-does-t-double-ampersand-mean-in-c11) – Trevor Boyd Smith Sep 04 '17 at 13:25
  • The duplicate should be the last one, this question was asked 4 months before. – Ictus Nov 02 '21 at 15:29

6 Answers6

331

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

Urmas Rahu
  • 313
  • 1
  • 8
Xinsong Lin
  • 3,422
  • 2
  • 9
  • 12
  • 25
    The only example I wish you'd add is how this works with std::move. Showing what would happen with `int&& c = std::move( b );`, etc. – AlwaysTalkingAboutMyDog Jul 24 '19 at 21:00
  • 31
    It looks like [Sravani S](https://www.tutorialspoint.com/answers/Sravani-S) blatantly plagiarized from you for TutorialsPoint on 15 Feb. 2018, [here](https://www.tutorialspoint.com/What-is-double-address-operator-and-and-in-Cplusplus). – Gabriel Staples Apr 24 '20 at 23:52
  • 19
    I just sent TutorialsPoint [this message](https://i.stack.imgur.com/GPEYt.png). The article, as plagiarized by them, [looks like this](https://i.stack.imgur.com/n40nr.png). – Gabriel Staples Apr 24 '20 at 23:58
  • if `foo(b); ` is error, how vector.push_back works in c++11?? – subash Apr 24 '21 at 18:20
  • 2
    There is another overload `void push_back (const value_type& val);` that takes an l-value. – Xinsong Lin Apr 24 '21 at 20:43
  • 1
    The way I remember l- and r-values is by thinking about the left- and right-side arguments to an assignment operator. In `lefty = righty`, lefty needs to be an l-value and righty is used as an r-value. – Peter Raynham Mar 24 '22 at 22:54
  • 8
    It's 2022 and TutorialsPoint still shows the plagiarized content. – Ricardo Cárdenes Apr 15 '22 at 17:02
137

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

Community
  • 1
  • 1
aschepler
  • 70,891
  • 9
  • 107
  • 161
110

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

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
41

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.

user1050755
  • 11,218
  • 4
  • 45
  • 56
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
6

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

yash101
  • 661
  • 1
  • 8
  • 20
  • 1
    What is your answer adding to this 4 year old question, which hasn't already been covered in the other answers? – Masked Man Dec 23 '14 at 17:12
  • 7
    I didn't notice any answer like this so I decided to add on. I came across this question by googling just today. – yash101 Dec 23 '14 at 18:36
2

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
{
//...
}