Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does && mean at the end of a function signature (after the closing parenthesis)? [duplicate]

Tags:

c++

In Workarounds for no 'rvalue references to *this' feature, I see the following member function (a conversion operator):

template< class T > struct A {     operator T&&() && // <-- What does the second '&&' mean?     {         // ...     } }; 

What does the second pair of && mean? I am not familiar with that syntax.

like image 585
Dan Nissenbaum Avatar asked Mar 10 '13 07:03

Dan Nissenbaum


People also ask

What does the fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What does a real fox say?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.

How can I identify a song by sound?

On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.


2 Answers

This is a ref-value qualifier. Here is a basic example:

// t.cpp #include <iostream>  struct test{   void f() &{ std::cout << "lvalue object\n"; }   void f() &&{ std::cout << "rvalue object\n"; } };  int main(){   test t;   t.f(); // lvalue   test().f(); // rvalue } 

Output:

$ clang++ -std=c++0x -stdlib=libc++ -Wall -pedantic t.cpp $ ./a.out lvalue object rvalue object 

Taken from here.

like image 199
crazylpfan Avatar answered Oct 11 '22 15:10

crazylpfan


It indicates that the function can be invoked only on rvalues.

struct X {       //can be invoked on lvalue       void f() & { std::cout << "f() &" << std::endl; }        //can be invoked on rvalue       void f() && { std::cout << "f() &&" << std::endl; } };  X x;  x.f();  //invokes the first function         //because x is a named object, hence lvalue  X().f(); //invokes the second function           //because X() is an unnamed object, hence rvalue 

Live Demo output:

f() & f() && 

Hope that helps.

like image 29
Nawaz Avatar answered Oct 11 '22 13:10

Nawaz