lambda捕获支持varidiac模板参数 [英] Does lambda capture support varidiac template arguments

查看:544
本文介绍了lambda捕获支持varidiac模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是很好,如果我可以做以下:

 模板< class RT,class ... PT& 
class Event
{
...
void operator()(PT ... args)
{
std :: for_each b l.begin(),l.end(),[args ...](Handler * p){(* p)(args ...);}
);
}
...
};不幸的是我无法使用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:error: $ b evtempl.hh:75:57:error:参数包不用'...'扩展:
evtempl.hh:75:57:note:'args'
evtempl.hh:In lambda函数:
evtempl.hh:76:26:错误:扩展模式'args'不包含任何参数包
evtempl.hh:在'void elt :: Event :: operator() ..)[with 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 ...) void; ':
testevtempl.cc:29:20:从这里需要
evtempl.hh:74:9:错误:使用无效字段'elt :: Event: :operator()(PT ...):::: Handler *)> :: __ args'


将lambda改为旧的,世俗的语法:

  for(itr = l.begin(); itr!= l.end (); ++ itr)
(*(* itr))(args ...);

这个编译工作正常。



我不知道为什么lambda语法不起作用。


  1. 我做错了什么或错过了什么?

  2. 这种东西在c ++ 11标准中是否被禁止?

  3. 或者,这是标准允许的,但是当前编译器的
    a问题?

我尝试过

  [=](Handler * p){(* p)(args ...) ; } 

它给出的错误与您所做的一样:

  [args](Handler * p){(* p)(args ...); } 

抱怨参数包未展开

解决方案

这是gcc中的一个错误。请参见 [c ++ 0x] lambdas和可变参数模板不能同时使用或可能 [C ++ 11]包扩展在lambda表达式中失败


It would be nice if I could do the following:

template <class RT, class... PT>
class Event 
{
    ...
    void operator()(PT... args)
    {
        std::for_each(
            l.begin(), l.end(), [args...](Handler *p) { (*p)(args...); }
        );
    }
    ...
};

Unfortunately I couldn't make it compile with g++ 4.7.2 (-std=c++0x):

evtempl.hh: In member function 'void elt::Event::operator()(PT ...)': evtempl.hh:75:54: error: expected ',' before '...' token evtempl.hh:75:54: error: expected identifier before '...' token evtempl.hh:75:57: error: parameter packs not expanded with '...': evtempl.hh:75:57: note: 'args' evtempl.hh: In lambda function: evtempl.hh:76:26: error: expansion pattern 'args' contains no argument packs evtempl.hh: In instantiation of 'void elt::Event::operator()(PT ...) [with RT = void; PT = {int}]': testevtempl.cc:28:9: required from here evtempl.hh:74:9: error: using invalid field 'elt::Event::operator()(PT ...)::::Handler*)>::__args' evtempl.hh: In instantiation of 'void elt::Event::operator()(PT ...) [with RT = void; PT = {int, const char*}]': testevtempl.cc:29:20: required from here evtempl.hh:74:9: error: using invalid field 'elt::Event::operator()(PT ...)::::Handler*)>::__args'

instead, I have to change that lambda to the old, mundane syntax:

for (itr = l.begin(); itr != l.end(); ++itr)
     (*(*itr))(args...);

This one compiles and works fine.

I wonder why the lambda syntax doesn't work.

  1. Did I do something wrong or miss something?
  2. Is such thing outlawed in the c++11 standard?
  3. or, this is allowed by the standard, but it's a problem of the current compiler?

I tried

[=](Handler *p) { (*p)(args...); }

it gives the same error as if you did:

[args](Handler *p) { (*p)(args...); }

complaining parameter packs not expanded

解决方案

It's a bug in gcc. See [c++0x]lambdas and variadic templates don't work together or perhaps [C++11] Pack expansion fails in lambda expressions.

这篇关于lambda捕获支持varidiac模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆