加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 11 decltype成员

发布时间:2020-12-16 09:54:34 所属栏目:百科 来源:网络整理
导读:为什么我不能这样做: class Foo { void fn(); using fn_t = decltype(fn); //call to non-static member function without an object argument}; 但我能做到 class Foo { static void fn(); using fn_t = decltype(fn);}; This SO post声称: Within uneval
为什么我不能这样做:

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

但我能做到

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

This SO post声称:

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

解决方法

fn是表示非静态成员函数的有效id-expression. §5.1.1[expr.prim.general] / p13(省略脚注):

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).

由于decltype不是可以使用表示非静态成员函数的id表达式的少数上下文之一,因此该程序是不正确的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读