【发布时间】:2015-11-23 10:17:35
【问题描述】:
我有这个代码:
#include <iostream>
using namespace std;
int print(int i)
{
cout << endl << i;
}
template<typename ...Args>
inline void pass(Args&&...args)
{
}
template<typename ...args>
inline void expand(args&&... a)
{
print(a) ...; //this doesn't expand
//pass( print(a)... ); this works
}
int main() {
expand(1,2,3,4);
return 0;
}
它会抛出一个错误:
In function 'void expand(args&& ...)':
error: expected ';' before '...' token
print(a) ...;
^
parameter packs not expanded with '...':
print(a) ...;
^
为什么需要使用pass() 函数?
【问题讨论】:
-
在允许逗号分隔列表的情况下,基本上允许参数包扩展。有固定数量的上下文:函数参数列表、模板参数列表、初始化器等。表达式不是有效的上下文。逗号运算符不会生成逗号分隔的列表。
标签: c++ c++14 variadic-templates variadic-functions