Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'auto a_class::f(...) -> T const' ambiguous in the grammar?

How should the below member function prototype be interpreted in C++11?

class C {
 public:
  auto f(...) -> T const;
}

It would seem to me that it could either be a const member function of the class C, or a non-const member function which returns a const value of type T.

I know I could just write the function as

T const f(...);

or

T f(...) const;

However, I want to be consistent with how I declare functions, so I decided to use the new C++11 auto f(...) -> RetType way everywhere.

like image 903
Kellen McClain Avatar asked Dec 23 '12 22:12

Kellen McClain


1 Answers

The trailing-return-type comes after cv- and ref-qualifiers of a non-static member function. This means the example in the question is the same as T const f(...);.

§8.4.1 [dcl.fct.def.general] p2

The declarator in a function-definition shall have the form

D1 ( parameter-declaration-clause ) cv-qualifier-seqopt ref-qualifieropt exception-specificationopt attribute-specifier-seqopt trailing-return-typeopt

To declare a const member function, you'd write auto f(...) const -> T const;.

like image 64
Xeo Avatar answered Oct 31 '22 12:10

Xeo