簡體   English   中英

G ++ std :: function問題

[英]G++ problems with std::function

我有以下代碼:

#include <functional>

std::function<int,int> p;

int main()
{

return 0;
}

我正在使用MinGW g ++ 4.8.1,失敗了

C:\main.cpp|4|error: wrong number of template arguments (2, should be 1)|

c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\functional|1866|error: provided for 'template<class _Signature> class std::function'|

C:\main.cpp|4|error: invalid type in declaration before ';' token|

這是G ++錯誤還是我使用std :: function不正確

std::function<int(int)>接受int並返回int。 例如

int foo(int);

std::function<void(int,int)>帶有兩個整數,並且沒有返回值。 例如

void foo(int, int);

std::function接受一個模板參數-它包裝的可調用對象的類型。 所以,如果你想構建std::function返回類型Ret和需要的類型參數Arg1, Arg2,..., ArgN ,你會寫std::function<Ret(Arg1, Arg2,..., ArgN)>

(請注意,橢圓並不是要指示參數包的擴展,它們只是在常規數學意義上使用。)

正如編譯器所說,std :: function接受一個模板參數。

使用語法returntype(argtype,...)

int foo(int a, int b) { return a+b; }
std::function<int(int,int)> p = foo;

int bar(int a) { return ++a; }
std::function<int(int)> q = bar;

void boo() { return; }
std::function<void()> r = boo;

暫無
暫無

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

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