:: std :: initializer_list与可变参数模板 [英] ::std::initializer_list vs variadic templates

查看:62
本文介绍了:: std :: initializer_list与可变参数模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 :: std :: initializer_list 传递多个参数是否比可变函数模板方法具有任何优势?

Does passing multiple arguments via ::std::initializer_list offer any advantages over the variadic function template method?

在代码中:

template <typename T> void f(::std::initializer_list<T> const);

template <typename ...A> void f(A&& ...args);

请注意,也可以通过SFINAE或 static_assert()将类型 A ... 限制为单个类型.可以通过...来迭代参数.

Note that the types A... can be restricted to a single type as well, through SFINAE or static_assert(). The arguments can be iterated through ...

推荐答案

您可以以无法遍历参数包的方式遍历 std :: initializer_list (除非您拥有C ++)17,然后先将其折叠).因此,例如,您可以使用列表的 for 循环.对于参数包,只能通过递归专用定义来扩展它们.

You can iterate over an std::initializer_list in a way that you cannot over a parameter pack (unless you have C++17 and you fold it first). So for example you can have for loops with the lists. With parameter packs, you only option to expand them is via recursive specialised definitions.

这篇关于:: std :: initializer_list与可变参数模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆