234

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?

5
  • 4
    Maybe it's an address of a reference.
    – Gabe
    Dec 28, 2010 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, 2010 at 20:17
  • 7
    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, 2010 at 20:20
  • 3
    Possible duplicate of What does T&& (double ampersand) mean in C++11? Sep 4, 2017 at 13:25
  • The duplicate should be the last one, this question was asked 4 months before.
    – Ictus
    Nov 2, 2021 at 15:29

6 Answers 6

345

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

8
  • 26
    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. Jul 24, 2019 at 21:00
  • 32
    It looks like Sravani S blatantly plagiarized from you for TutorialsPoint on 15 Feb. 2018, here. Apr 24, 2020 at 23:52
  • 20
    I just sent TutorialsPoint this message. The article, as plagiarized by them, looks like this. Apr 24, 2020 at 23:58
  • 2
    There is another overload void push_back (const value_type& val); that takes an l-value. Apr 24, 2021 at 20:43
  • 10
    It's 2022 and TutorialsPoint still shows the plagiarized content. Apr 15, 2022 at 17:02
138

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

1
  • 3
    really? this is a nice explanation! X means Y. But what the heck Y means? oops! Not explained! Oct 15, 2023 at 21:36
111

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

0
42

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.

5
  • The && token itself is not new; its meaning in declarations is. But you knew that.
    – aschepler
    Dec 28, 2010 at 20:47
  • It's new in this context. But it is highly traditional for C/C++ to overload its tokens to have different meaning in different contexts. Dec 28, 2010 at 21:16
  • 1
    @aschkleper - of course... the logical-and operator didn't even enter my mind. I'll update the answer. Dec 28, 2010 at 21:44
  • Broken Link... So answer is worthless.
    – Jiminion
    Mar 22, 2021 at 19:53
  • 3
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

2
  • 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, 2014 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, 2014 at 18:36
3

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

20/02/24: I felt that the && question was relevant to move constructors and is missing explainations on lvalues, rvalues, move semantics and std::move.

I've included a decent video from the Cherno that explains the && better than StackOverflow comments.

https://www.youtube.com/watch?v=fbYknr-HPYE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.