首先,所有成员访问表达式都由编译器转换:
struct X{
int a;
void f(){}
void g(int b){
int x = a + b; // actually: int x = (*this).a + b
f(); // actually: (*this).f();
}
};
§9.3.1 [class.mfct.non-static] p3
[...] id-expression(*this)
使用(9.3.2) 作为运算符左侧的后缀表达式转换为类成员访问表达式 (5.2.5) .
。[...]
现在,标准中的示例在尾随返回类型中调用另一个成员函数主体之外的成员函数。这个电话也被改变了:
template<class T> auto f(T t) -> decltype(t + (*this).g())
{ return t + (*this).g(); }
而在成员函数体之外,*this
显然是一个不完整的类型。这意味着您只能访问在使用之前已声明的名称 - 但该部分不仅适用于*this
使用:
struct X{
using typeA = int;
typeA f(); // OK, 'typeA' has been declared before
typeB g(); // Error: 'typeB' not declared before usage
using typeB = float;
};