【发布时间】:2025-12-15 16:20:17
【问题描述】:
解释 decltype 关键字如何工作的规则之一是:
如果参数是 [...] 无括号的类成员访问表达式,则 @987654323 @ 产生由此表达式命名的实体的类型。 (https://en.cppreference.com/w/cpp/language/decltype)
但是,请考虑以下示例(为简洁起见,使用伪代码):
struct X {
int a;
};
X x;
int X::* ptr = &X::a;
std::is_lvalue_reference<decltype(x.*ptr)>::value; //Evaluates to 1. The type is int&
std::is_rvalue_reference<decltype(X().*ptr)>::value; //Evaluates to 1. The type is int&&
这意味着通过指向成员的指针访问类成员不是类成员访问表达式。它被视为只是一个普通表达式,我引用的规则不适用。
因此,我的问题是:对于decltype,我怎么知道什么才是类成员访问表达式?我应该寻找什么来源才能找到准确的定义?
【问题讨论】:
标签: c++ templates types language-lawyer c++20