gpt4 book ai didi

成员的 C++11 decltype

转载 作者:太空狗 更新时间:2023-10-29 20:12:42 24 4
gpt4 key购买 nike

为什么我不能这样做:

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-expression 的少数上下文之一,因此程序格式错误。

关于成员的 C++11 decltype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26199545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com