繁体   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