2014-12-15 - rvalue to this

rvalue reference to this allows you to create member functions, callable if this is in fact rvalue. it is similar to const member functions. for member function foo() a syntax would like this:

void foo() &&;

nice and straight forward. but that's where const-analogy breaks. for const and non-const overloads one does:

void foo();
void foo() const;

now let's try it with rvalue:

void foo();
void foo() &&;

and now computer says “no”:

error: ‘void X::foo() &&’ cannot be overloaded
 void foo() &&;
      ^
error: with ‘void X::foo()’
 void foo();
      ^

what is going on? at first i though this feature is not yet implemented on GCC. after double checking – it IS implemented… so let's try clang:

error: cannot overload a member function with ref-qualifier '&&' with a member function without a ref-qualifier
  void foo() &&;
       ^                    
note: previous declaration is here
  void foo();                    
       ^

as usually clang is a bit more human friendly here. already see the problem? code should look like this:

void foo() &;  // <-- '&' here...
void foo() &&;

it appears that member functions, when overload for ref, need proper ref-qualifier on both overloads! why it is so? it is still a mystery to me…

btw: if you wonder about this:

void foo();
void foo() &;
void foo() &&;

no – it does not work:

error: cannot overload a member function with ref-qualifier '&' with a member function without a ref-qualifier
blog/2014/12/15/rvalue_to_this.txt · Last modified: 2014/12/15 21:33 by basz
Back to top
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0