28

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.

Community
  • 1
  • 1
Dan Nissenbaum
  • 12,293
  • 18
  • 99
  • 168
  • 2
    **&& ref-qualifier**: all declarations `T()` have a ref-qualifier: [**Link**](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm) – Grijesh Chauhan Mar 10 '13 at 07:41

2 Answers2

28

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.

Community
  • 1
  • 1
crazylpfan
  • 842
  • 5
  • 9
22

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.

Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • Yes I found [*rvalue-reference to cv X" for functions declared with the && ref-qualifier*](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm) 13.3.1 – Grijesh Chauhan Mar 10 '13 at 07:43
  • Note that according to http://stackoverflow.com/a/8610714/368896, it's not legal to overload using `f()` and `f() &&` - "you are not allowed to overload between the r-value reference versions and the non-reference versions". – Dan Nissenbaum Mar 10 '13 at 07:57
  • @DanNissenbaum: Yes, my LWS demo shows that. But because of power failure, I couldn't post that immediately. Updated it now. – Nawaz Mar 10 '13 at 08:01
  • Thanks. What does LWS mean? I cannot find it via Google - very few results appear for "LWS". – Dan Nissenbaum Mar 10 '13 at 08:04
  • LWS stands for [www.liveworkspace.org](http://liveworkspace.org/code/4Gr0VK%244) where I've shown the demo. – Nawaz Mar 10 '13 at 08:06