0
为什么this
在静态成员函数中未经评估的上下文中是不允许的?静态成员函数中未评估上下文中的这种情况
struct A
{
void f() {}
static void callback(void * self) // passed to C function
{
static_cast< decltype(this) >(self)->f();
}
};
此代码给出一个错误:
error: 'this' is unavailable for static member functions
static_cast< decltype(this) >(self)->f(); ^~~~
decltype(this)
需要有为了简洁(有时它是更短,则VeryVeryLongClassName *
),另一个优点是以下事实:意图更加清晰。
什么标准说关于在静态成员函数的未评估上下文中使用this
?
([在静态成员函数封闭类的C++型]的可能的复制https://stackoverflow.com/questions/21143275/c-type-of-enclosing-class-in-static-member-function ) –