See example and discussion on https://stackoverflow.com/questions/41954449/what-does-gccs-conversion-function-operator-int-do Conversion member function declaration like &operator int (); does not produce a warning or error and is interpreted as operator int& (); by GCC. It fails on e.g. CLANG. Usage of & in front of operator seems not to be covered by the language definition. Verified up to gcc 6.3.
Full example: #include <iostream> using namespace std; class Foo { private: int a; public: Foo():a(0){}; &operator int() {return a;}; // this fails with Clang }; int main() { Foo foo; int x = foo; cout << x << endl; }
Smaller testcase: struct A { int a; A () : a(0) {}; &operator int () { return a; }; }; int bar () { A a; return a; } clang++ indeed reports: pr79318.C:5:3: error: cannot specify any part of a return type in the declaration of a conversion function; put the complete type after 'operator' &operator int () { return a; }; ^ & [dcl.dcl]/12 indeed requires that conversion functions are declared with nodeclspec-function-declaration.
This seems to be http://wg21.link/cwg1726
Confirmed. The defect report has been in drafting since 2013. Maybe it was forgotten about. I assume it is supposed to be rejected too.
CWG 1726 is now a DR.