如果我能做到以下几点会很好:
template <class RT, class... PT>
class Event
{
...
void operator()(PT... args)
{
std::for_each(
l.begin(), l.end(), [args...](Handler *p) { (*p)(args...); }
);
}
...
};
Run Code Online (Sandbox Code Playgroud)
不幸的是我无法使用g ++ 4.7.2(-std = c ++ 0x)进行编译:
evtempl.hh:在成员函数'void elt :: Event :: operator()(PT ...)':evtempl.hh:75:54:error:expected','before'...'token evtempl.hh :75:54:错误:'...'之前的预期标识符evtempl.hh:75:57:错误:参数包未用'...'扩展:evtempl.hh:75:57:注意:'args' evtempl.hh:在lambda函数中:evtempl.hh:76:26:错误:扩展模式'args'不包含参数包evtempl.hh:在'void elt :: Event :: operator()的实例化中(PT ... )[RT = void; PT = {int}]':testevtempl.cc:28:9:从这里需要evtempl.hh:74:9:错误:使用无效字段'elt :: Event :: operator()(PT ...):: :: Handler*)> :: __ args'evtempl.hh:实例化'void elt :: Event :: operator()(PT ...)[with RT = void; PT = {int,const char*}]':testevtempl.cc:29:20:从这里需要evtempl.hh:74:9:错误:使用无效字段'elt :: Event :: operator()(PT .. .)::::处理器*)> :: __ ARGS'
相反,我必须将lambda更改为旧的,平凡的语法:
for (itr = l.begin(); itr != l.end(); ++itr)
(*(*itr))(args...);
Run Code Online (Sandbox Code Playgroud)
这个编译并正常工作.
我想知道为什么lambda语法不起作用.
我试过了
[=](Handler *p) { (*p)(args...); }
Run Code Online (Sandbox Code Playgroud)
它会给出与您相同的错误:
[args](Handler *p) { (*p)(args...); }
Run Code Online (Sandbox Code Playgroud)
抱怨的参数包没有扩展
| 归档时间: |
|
| 查看次数: |
2192 次 |
| 最近记录: |