3
是否可以使用由模板參數定義的類型的構造函數構造std::function
?使用基於模板的構造函數構造std :: function
例如:
template <typename T>
bool registerType()
{
const std::function<T()> func = &T::T; //I know this doesn't work
//...
}
是否可以使用由模板參數定義的類型的構造函數構造std::function
?使用基於模板的構造函數構造std :: function
例如:
template <typename T>
bool registerType()
{
const std::function<T()> func = &T::T; //I know this doesn't work
//...
}
我不這麼認爲,因爲構造函數沒有名字,你不能把一個指針/引用他們,一般他們不會表現得很像功能。
你可以使用一個拉姆達初始化std::function
具有相同簽名:
const std::function<T()> func = [](void) { return T(); } // or something like that
調用它產生相同的結果作爲使用表達式T()
構建臨時T
型的,但有可能進行不同的副作用效果。在真正的函數調用的情況下,在return語句中有一個額外的臨時變量,它名義上被複制/移動到返回值。實施可能會或可能不會暫時停止。