简体   繁体   English

G ++ std :: function问题

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

I have the following code: 我有以下代码:

#include <functional>

std::function<int,int> p;

int main()
{

return 0;
}

I am using MinGW g++ 4.8.1 which fails with 我正在使用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|

is this a G++ bug or am I using std::function incorrectly 这是G ++错误还是我使用std :: function不正确

std::function<int(int)> for function takes int and return int. std::function<int(int)>接受int并返回int。 eg 例如

int foo(int);

std::function<void(int,int)> for function takes two ints and no return value. std::function<void(int,int)>带有两个整数,并且没有返回值。 eg 例如

void foo(int, int);

std::function takes one template argument - the type of the callable object that it wraps around. std::function接受一个模板参数-它包装的可调用对象的类型。 So, if you want to construct an std::function which returns type Ret and takes arguments of types Arg1, Arg2,..., ArgN , you would write std::function<Ret(Arg1, Arg2,..., ArgN)> . 所以,如果你想构建std::function返回类型Ret和需要的类型参数Arg1, Arg2,..., ArgN ,你会写std::function<Ret(Arg1, Arg2,..., ArgN)>

(Note that the ellipses weren't meant to indicate parameter pack expansion - they are just being used in the regular mathematical sense.) (请注意,椭圆并不是要指示参数包的扩展,它们只是在常规数学意义上使用。)

As the compiler says, std::function takes one template argument. 正如编译器所说,std :: function接受一个模板参数。

Use the syntax returntype(argtype, ...) 使用语法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