2017-07-03 72 views
1

我試圖創建一個宏來安裝和刪除從lambda構造的Qt事件過濾器。在這種情況下,this被稱爲QObject,因此具有成員destroyedfilter只是一些QObject衍生的事件過濾器。但是我有一個行一個問題:獲取帶有decltype的函數引用(this)

connect(this, &decltype(this)::destroyed, [filter]() 
{ 
    qApp->removeEventFilter(filter); 
    filter->deleteLater(); 
}); 

這給(MSVC2013)錯誤:

left of '::' must be a class, struct or union

我只是得到了語法錯誤,或者我不能這樣做呢?

+2

'this'是一個指針。 – molbdnilo

+0

將''using''語句(定義''decltype(* this)''作爲別名)放在之前並使用別名代替 – cmdLP

回答

3

Per @ molbdnilo的評論,我沒有考慮到this是一個指針的事實。使用類型特徵去除指針使其工作:

connect(this, &std::remove_pointer<decltype(this)>::type::destroyed, [filter]() 
{ 
    qApp->removeEventFilter(filter); 
    filter->deleteLater(); 
}); 
+0

值得注意的是,C++ 14引入了'std :: remove_pointer_t ' typename std :: remove_pointer :: type'。 –

+0

@FrançoisAndrieux感謝您指出了這一點,但我仍然堅持使用msvc2013/C++ 11來完成這項特定任務! –

+1

@NicolasHolthaus:爲什麼不使用'&decltype(* this):: destroyed'?或者甚至只是'摧毀',因爲你顯然已經在一個類中定義'destroy()' –