引入了 C++11 variadic templates
template <typename... Args>
void foo(Args... params) {
cout << sizeof...(Args) << endl;
}
Args
和params
的名称是什么?我知道其中之一(至少?)被称为可变参数模板包,但它是哪一个?另一个叫什么?
最佳答案
部分引用 FDIS,§14.5.3:
1 A template parameter pack is a template parameter that accepts zero or more template arguments.
2 A function parameter pack is a function parameter that accepts zero or more function arguments.
3 A parameter pack is either a template parameter pack or a function parameter pack.
4 A pack expansion consists of a pattern and an ellipsis, the instantiation of which produces zero or more instantiations of the pattern in a list.
所以在你的例子中,
typename...Args
是一个模板参数包(因此也是一个参数包)Args...params
是一个函数参数包(因此也是一个参数包)sizeof...(Args)
是一个包扩展,其中Args
是模式(一个标识符在这种情况下)。
关于c++ - 为可变参数模板添加的新语法实体的名称是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7972263/