为什么升压MPL有整型常量? [英] Why does Boost MPL have integral constants?

查看:173
本文介绍了为什么升压MPL有整型常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

既然你可以把积分值作为模板参数并对其执行算术,什么是落后的boost :: MPL :: int_&LT的动机;>和其他整型常量?这是否仍然动机适用于C ++ 11?

Since you can take integral values as template parameters and perform arithmetic on them, what's the motivation behind boost::mpl::int_<> and other integral constants? Does this motivation still apply in C++11?

推荐答案

您可以把积分值作为模板参数,但你不能把两者的类型的和的非类型模板参数使用单个模板。长话短说,治疗的非类型模板参数的作为的类型的允许他们对事物内部的 MPL 的无数被使用。

You can take integral values as template parameters, but you cannot take both types and non-type template parameters with a single template. Long story short, treating non-type template parameters as types allows for them to be used with a myriad of things within MPL.

例如,考虑一元函数找到与类型的作品,并寻找一个序列中一个平等类型。如果您想与使用它的非类型模板参数的你就需要重新实现新的算法,过载,一个 find_c 为你必须手动指定的积分值的的键入的。现在想象你想让它混合组成的工种的作为语言的休息呢,还是要搭配的类型的和的非类型,你'超载'也正好是很难使用,你必须到处指定每个非类型参数的类型的爆炸。

For instance, consider a metafunction find that works with types and looks for an equal type within a sequence. If you wished to use it with non-type template parameters you would need to reimplement new algorithms 'overloads', a find_c for which you have to manually specify the type of the integral value. Now imagine you want it to work with mixed integral types as the rest of the language does, or that you want to mix types and non-types, you get an explosion of 'overloads' that also happen to be harder to use as you have to specify the type of each non-type parameter everywhere.

这动机确实还适用于的 C ++ 11

This motivation does still apply in C++11.

这动机仍适用于的 C ++是的和其他任何版本,除非我们有一些新的规则,允许从转换的非类型模板参数键入的模板参数。举例来说,当你使用 5 和模板请求类型与初始化它的std :: integral_constant&LT; INT,5方式&gt; 而不是

This motivation will still apply to C++y and any other version, unless we have some new rule that allows conversion from non-type template parameters to type template parameters. For instance, whenever you use 5 and the template requests a type instantiate it with std::integral_constant< int, 5 > instead.

这篇关于为什么升压MPL有整型常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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