简体   繁体   中英

C++11 decltype of 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 claims:

Within unevaluated operands (operands of decltype, sizeof, noexcept, ...) you can name nonstatic data members also outside of member functions

fn is a valid id-expression denoting a non-static member function. §5.1.1 [expr.prim.general]/p13 (footnote omitted):

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

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

§7.1.6.2 [dcl.type.simple]/p4:

The operand of the decltype specifier is an unevaluated operand (Clause 5).

Since decltype is not one of the few contexts in which an id-expression denoting a non-static member function may be used, the program is ill-formed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM