【发布时间】:2011-12-14 04:56:42
【问题描述】:
我看到了一个blog post,它使用了非类型可变参数模板(gcc 目前不支持,只有 clang 支持)。
template <class T, size_t... Dimensions>
struct MultiDimArray { /* ... */ };
帖子中的示例编译良好,但我无法让它与函数模板一起使用。
谁能帮忙找出正确的语法(如果存在的话)?
int max(int n) { return n; } // end condition
template <int... N> // replacing int... with typename... works
int max(int n, N... rest) // !! error: unknown type name 'N'
{
int tmp = max(rest...);
return n < tmp? tmp : n;
}
#include <iostream>
int main()
{
std::cout << max(3, 1, 4, 2, 5, 0) << std::endl;
}
【问题讨论】:
-
我认为标准库中已经存在可变参数
max...但我不完全确定。 -
@Kerrek
max不是重点,只是一个例子。 -
你想要一个非编译时函数(即
max(3,1,4,foo(),5,bar())而不是max<3,1,4,2,5,6>()),它接受任意数量的参数,所有同一类型?哪个返回最大值? -
@AaronMcDaid,是的,我正在寻找一个非编译时函数。多亏了 Konrad 和 Luc 的解释,我已经看到了我的方式的错误。
-
等等,为什么你说 gcc 不支持这个?使用 4.9 编译良好span>
标签: c++ c++11 variadic-templates