[英]Is there a reason we cannot name a non-static member function in an unevaluated context?
[英]this in unevaluated context in static member functions
為什么在靜態成員函數的未評估上下文中不允許this
做?
struct A
{
void f() {}
static void callback(void * self) // passed to C function
{
static_cast< decltype(this) >(self)->f();
}
};
這段代碼給出了一個錯誤:
錯誤:“此”不適用於靜態成員函數
static_cast< decltype(this) >(self)->f(); ^~~~
decltype(this)
為了簡潔起見是需要的(有時它要短得多,然后是VeryVeryLongClassName *
),另一個優點是意圖更清晰。
對於在靜態成員函數的未評估上下文中使用this
標准,標准說了什么?
我不明白它是如何重要的是this
會出現一個未計算的環境中,你提到的東西, 不存在一個靜態成員函數,所以編譯器應該如何演繹的類型this
此范圍內?
作為推論,非靜態成員函數中this
的類型取決於所述成員函數的cv-qualifier,如果成員函數為const
,則decltype(this)
將產生T const*
如果不是,則為T *
t。 因此,類型取決於表達式的上下文。 在您的示例中,上下文沒有this
指針。
為了減輕必須為類命名的麻煩,您可以為其添加別名。
class VeryVeryLongClassName
{
using self = VeryVeryLongClassName;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.