一段有关std:function函数指针代码编译异常
一段有关std:function函数指针代码编译错误
代码如下
报错信息如下
编译环境是Gcc/G++ 4.4
boost版本1.43.0
libstdc++版本4.4
想问问这些错误的解决方法,还有就是这一句的[&]是什么用法?
std::function<void(int)> f2 = [&](int i)->void {
这个void后面的我感觉是个函数定义,但函数没有名字,是不是匿名函数呢?求详解。
------解决方案--------------------
这是 lambda表达式
GCC4.5 才支持,升级GCC版本吧
代码如下
#include <functional>
template <class HContext>
std::function<void(int)> task_wrapper(std::function<void(int)> f, HContext c) {
std::function<void(int)> f2 = [&](int i)->void {
hsl_before_execute(c);
f(i);
hsl_after_execute(c);
};
return f2;
}
报错信息如下
hsl_temp.h: In function ‘std::function<void(int)> task_wrapper(std::function<void(int)>, HContext)’:
hsl_temp.h:103: error: expected primary-expression before ‘[’ token
hsl_temp.h:103: error: expected primary-expression before ‘]’ token
hsl_temp.h:103: error: expected primary-expression before ‘int’
hsl_temp.h:103: error: expected unqualified-id before ‘void’
hsl_temp.h:103: error: expected ‘,’ or ‘;’ before ‘void’
编译环境是Gcc/G++ 4.4
boost版本1.43.0
libstdc++版本4.4
想问问这些错误的解决方法,还有就是这一句的[&]是什么用法?
std::function<void(int)> f2 = [&](int i)->void {
这个void后面的我感觉是个函数定义,但函数没有名字,是不是匿名函数呢?求详解。
------解决方案--------------------
这是 lambda表达式
GCC4.5 才支持,升级GCC版本吧