C ++ 11 decltype member

Why can't I do this:

class Foo {
  void fn();
  using fn_t = decltype(fn); //call to non-static member function without an object argument
};

      

But I can do

class Foo {
  static void fn();
  using fn_t = decltype(fn);
};

      

This SO post states:

Inside non-operable operands (operands decltype, sizeof, noexcept, ...) you can name non-static data members also outside of member functions

+3


source to share


1 answer


fn

is a valid id expression that denotes a non-stationary member function. ยง5.1.1 [expr.prim.general] / p13 (footnote omitted):

An identifier that denotes a non-static data member or non-static class member function can only be used:

  • as part of a member access of a class (5.2.5) in which an object expression refers to a member class or a class derived from that class, or
  • to form a pointer to an element (5.3.1) or
  • if the id expression denotes a non-static data item and it appears in an unvalued operand.

ยง7.1.6.2 [dcl.type.simple] / p4:



The operand of the qualifier decltype

is an unvalued operand (Item 5).

Since it is decltype

not one of the few contexts in which id can be used to denote a non-stationary member function, the program is ill-formed.

+7


source







All Articles