Koushik Shetty
Koushik Shetty

Reputation: 2176

ref qualifier gives error in gcc4.7.2 and vc10

consider the below minimal example.

#include<iostream>

struct A
{
    A(){std::cout<<"def"<<'\n';}
    void foo()&{std::cout<<"called on lvalue"<<'\n';}
};

int main()
{   
    A a;
    a.foo();
    A().foo();
    return 0;
}

this gives error about expecting ';' at the end of declaration and and expected un-qualified-id before '{'.

Can i know what i'm doing wrong? in the actual code i want to avoid calling the non-static member function through temporaries.

tried on gcc 4.7.2 and vc2010. Thanks.

Upvotes: 3

Views: 220

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126502

The answer will be short: VC10 and GCC 4.7.2 do not support ref-qualifiers.

However, notice that your foo() function has an lvalue ref qualifier, meaning that you cannot invoke it on temporaries.

If you want this expression to compile as well:

A().foo();

Then you should use const&, or provide an overload for &&, as in this live example.

To work with ref-qualifiers you can use Clang 3.2 or GCC 4.8.1.

Upvotes: 7

Related Questions