std::函数的G++问题
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:mingwlibgccmingw324.8.1includec++functional|1866|error: provided for 'template<class _Signature> class std::function'|
C:main.cpp|4|error: invalid type in declaration before ';' token|
这是一个G++错误,还是我使用std::函数不正确
std::function<int(int)>
取int并返回int。例如
int foo(int);
函数的std::function<void(int,int)>
取两个int并且没有返回值。例如
void foo(int, int);
std::function
采用一个模板参数,即它所封装的可调用对象的类型。因此,如果您想构造一个返回类型Ret
并接受类型Arg1, Arg2,..., ArgN
的参数的std::function
,那么您应该编写std::function<Ret(Arg1, Arg2,..., ArgN)>
。
(请注意,省略号并不是用来表示参数包扩展的,它们只是在常规数学意义上使用的。)
正如编译器所说,std::函数接受一个模板参数。
使用语法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;
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题