C++11 中的最小和最大可变参数模板变体?
我在阅读标准时是否正确,从 min
和 max
(以及 minmax
就此事而言)有新的 initializer_list 变体,但没有 Variadic Template 变体?
因此,这是可以的:
int a = min( { 1,2,a,b,5 } );
但这不是:
int b = min( 1,2,a,b,5 ); // err!
我想,很多人会期望可变参数模板可以轻松实现这一点,因此他们可能会感到失望。
我想说,使用 VT 来表示 min
和 max
有点过分了,
- 可变参数模板能够处理多种类型
- 初始化列表 em> 检查所有类型的设计是否相同,
因此 IL 更适合该任务。
我的解释正确吗?
Am I right in reading the standard that from min
and max
(and minmax
for that matter) there are new initializer_list variants, but no Variadic Template variants?
Thus, this is ok:
int a = min( { 1,2,a,b,5 } );
but this is not:
int b = min( 1,2,a,b,5 ); // err!
I guess, many people would expect that Variadic Templates could have easily implemented this, therefore they might be disappointed.
I'd say that using V.T. for min
and max
would be overkill
- variadic templates are capable of handling multiple types
- initializer lists check that all types are the same by design
therefore I.L. are much better suited for the task.
Is my interpretation correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的解释是正确的。 N2772 包含更深入的基本原理。
Your interpretation is correct. N2772 contains more in-depth rationale.
这是我的解决方案,使用带或不带 Boost Concepts Common Type Traits 的可变参数模板进行
min
在 GCC 4.6 上测试。不过,我不确定 common_type 是否需要。Here is my solution using variadic templates with and without Boost Concepts Common Type Traits for
min
tested on GCC 4.6. I'm not sure if common_type this is need or not, though.是的,我认为可以公平地说,让所有值都具有兼容类型使得列表成为此功能的良好候选者。这并不是说您不能编写自己的可变参数模板版本。
Yes I think it's fair to say that having all the values be of compatible types makes lists a good candidate for this feature. That's not to say that you couldn't write your own variadic template version.
通过结合可变参数模板和initializer_list,我们可以让函数 int b = min( 1,2,a,b,5 ); 无需递归展开即可工作。
By combining the variadic template and initializer_list, we can let the function
int b = min( 1,2,a,b,5 );
works without recursive unfolding.