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
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
std::function<int(int)>
for function takes int and return int. eg
int foo(int);
std::function<void(int,int)>
for function takes two ints and no return value. eg
void foo(int, int);
std::function
takes one template argument - the type of the callable object that it wraps around. 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)>
.
(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.
Use the syntax 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.