簡體   English   中英

可以使用functor作為std :: function的模板參數嗎?

[英]Is possible to use functor as template parameter to std::function?

我試圖找到用於編譯此示例c ++代碼的g ++編譯器參數:

#include <stdio.h>
#include <functional>

struct mystruct
{
    int a;
    int operator()(int y) { return y + 1; }
};

int main()
{
    std::function<mystruct> foo;
    return 0;
}

在cpp中,寫入了std :: function模板參數可以是“......或其他函數對象”。 我在包含幾個std :: function的大型項目上工作,而項目可以用g ++構建。 我試圖在VS2015下構建它,但是這些代碼上的編譯器抱怨:

錯誤C2027:在[_Fty = mystruct]中使用未定義類型'std :: _ Get_function_impl <_Fty>'

當我嘗試使用-std = c ++ 11在g ++下編譯上面的小樣本時,它也會抱怨:

錯誤:聚合'std :: function a'的類型不完整,無法定義

所以我認為在我們的大型可構建項目中,g ++可能已經切換了一些提供這種功能的擴展。

您可能將模板參數與要存儲的函數對象混合在一起。 我相信你想寫的是什么

std::function<int(int)> foo{mystruct{}};

答案是仿函數不能成為其他仿函數的模板參數。 感謝所有帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM