Variadic templates for lambda expressions(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 »...«
}
推荐答案
我想你也需要在捕获列表中展开pack 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:
捕获后跟省略号是包扩展(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 表达式的可变参数模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:lambda 表达式的可变参数模板
基础教程推荐
- centos 7 vscode cmake 编译c++工程 2023-09-17
- [c语言-函数]不定量参数 2023-09-08
- [C语言]二叉搜索树 2023-09-07
- 带你深度走入C语言取整以及4种函数 2022-09-17
- C语言编程C++旋转字符操作串示例详解 2022-11-20
- 全面了解C语言 static 关键字 2023-03-26
- C语言实现宾馆管理系统课程设计 2023-03-13
- C++实战之二进制数据处理与封装 2023-05-29
- C语言 详解字符串基础 2023-03-27
- C++实现ETW进行进程变动监控详解 2023-05-15
