为什么Boost MPL具有积分常数? [英] Why does Boost MPL have integral constants?

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

问题描述

因为你可以将整数值作为模板参数并对它们执行算术,boost :: mpl :: int_<>和其他积分常量的动机是什么?这个动机仍然适用于C ++ 11?

解决方案

你可以把整数值作为模板参数, 类型非类型模板参数。长篇小说,将非类型模板参数视为类型允许它们与 MPL 中的无数东西一起使用。 >

例如,考虑一个与类型一起使用的元函数 find ,并在序列中寻找一个等号类型。如果您希望使用非类型模板参数,您需要重新实现新算法的重载, find_c ,您必须手动指定积分值的类型。现在假设您希望像其他语言一样使用混合积分类型,或者您希望混合使用类型非类型,你会得到一个'重载'的爆炸,也很难使用,因为你必须指定每个非类型参数的类型到处。



这个动机仍然适用于 C ++ 11



其他版本,除非我们有一些允许从非类型模板参数转换为类型模板参数的新规则。例如,每当你使用 5 和模板请求一个类型实例化它与 std :: integral_constant< int,5>


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?

解决方案

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.

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.

This motivation does still apply in C++11.

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.

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

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