C++11 中的非类型可变参数函数模板
我看到一篇博客文章,其中使用了非type 可变参数模板(目前 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;
}
I saw a blog post which used non-type variadic templates (currently not supported by gcc, only by clang).
template <class T, size_t... Dimensions>
struct MultiDimArray { /* ... */ };
The example in the post compiles fine but I failed to get it to work with function templates.
Can anyone help figuring out the correct syntax (if such exists)?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将打印出所有元素,
get max 可以类似地实现
This will print out all elements,
get max could be implemented similarly
您只是混淆了类型名称和非类型名称。你想要的根本行不通。
您可以在函数中使用可变参数非类型模板,但不能作为(非模板)参数:
...虽然我还没有测试过这一点,并且我不确定如何 鉴于您需要部分专业化作为基本情况,这应该可行。您可以通过分派到部分专用的结构来解决此问题:
这会起作用。
You are simply confusing type names and non-type names. What you want simply doesn’t work.
You can probably use variadic non-type templates in functions, but not as (non-template) arguments:
… although I haven’t tested this and I’m not sure how this should work given that you need to have a partial specialisation as the base case. You could solve this by dispatching to a partially specialised struct:
This will work.
以下是定义仅接受
int
参数的可变参数函数模板的两种方法。第一个在实例化时生成硬错误,第二个使用 SFINAE:如您所见,此处不使用非类型模板参数。
Here are two ways of defining a variadic function template only accepting
int
parameters. The first one generates a hard-error when instantiated, the second uses SFINAE:As you can see, non-type template parameters aren't used here.
Luc Danton 的解决方案对于非
int
类型但可以隐式转换为int
的参数无法正确运行。下面是这样做的:这里,
max
将所有参数原封不动地转发给max_checked
,它采用相同数量的int
类型的参数(通过执行first_type
模板上的包扩展)。如果任何参数无法转换为int
,则使用decltype(...)
返回类型来应用 SFINAE。Luc Danton's solution doesn't behave correctly with parameters which are not of type
int
, but can be implicitly converted to anint
. Here's one which does:Here,
max
forwards all arguments unchanged tomax_checked
, which takes the same number of arguments of typeint
(provided by performing a pack-expansion on thefirst_type
template). Thedecltype(...)
return type is used to apply SFINAE if any argument can't be converted toint
.以下是如何在
max
示例中实现可变参数,以便它可以采用任意数量的算术参数:这很好,因为它可以采用任何数字类型,并且将按其原始类型返回最大数字,而不是不仅仅是
int
。Here's how you could achieve variadic args in your
max
example such that it can take any number of arithmetic arguments:This is good because it can take any numeric type and will return the maximum number by its original type rather than just
int
, too.