lambda表达式的可变模板 [英] Variadic templates for lambda expressions

查看:130
本文介绍了lambda表达式的可变模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用g ++执行此操作的正确方法是:

What's the correct way to do this with g++:

template < typename F >
void g (F f);

template < typename ... A >
void h (A ... a);

template < typename ... A >
void f (A ... a) {
  g ([&a] () { h (a...); }); // g++-4.6: error: parameter packs not expanded with »...«
}


推荐答案

我想你需要在捕获列表中展开 a ,如下:

I think you need to expand the pack a in the capture list as well, like this:

template < typename ... A >
void f (A ... a) {
  g ([&, a...] () { h (a...); }); 
}

这里是C ++ 0x委员会草案5.1.2.23:

Here is the relevant text from the C++0x Final Committee Draft, section 5.1.2.23:


捕获后跟一个省略号是一个
pack扩展(14.5.3)。 [示例:

A capture followed by an ellipsis is a pack expansion (14.5.3). [ Example:

template<class... Args> void f(Args... args) {
    auto lm = [&, args...] { return g(args...); }; lm();
}

- 结束示例]

这篇关于lambda表达式的可变模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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