一段有关std:function函数指针代码编译错误

richard_ma richard_ma 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下

#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后面的我感觉是个函数定义,但函数没有名字,是不是匿名函数呢?求详解。
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
richard_ma
C++ 语言 65,214 成员 250,514 内容