C++14 SFINAE 容器类value_type类型提升
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++14 SFINAE 容器类value_type类型提升,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3115字,纯文字阅读大概需要5分钟。
内容图文
![C++14 SFINAE 容器类value_type类型提升](/upload/InfoBanner/zyjiaocheng/1245/29c73efea67b4a23a6de49ed0c39858d.jpg)
C++14 SFINAE 容器类value_type类型提升
原问题:
已知容器类模板 Container及其value_type类型,返回容器类类型Container2,将原value_type按如下规则提升:
- bool,short int,int,long int,long long int,提升为long long int
- float,double,long double,提升为long double
- default,保持value_type不变
正文:
根据原问题易得如下结构:
template <???> ??? TypePromotion ???; template <class T> using Promotion = ??? TypePromotion ???; template <template <class> class Container, class T> struct ContainerPromotion { using Type = Container<Promotion<T> >; };
注解:
TypePromotion为一个待实现的type_traits设施,负责按照规则提升给定类型。
Promotion为TypePromotion的对外接口。
ContainerPromotion通过Promotion来定义提升后的容器类类型。
其中 TypePromotion需要找出给定类型所属的集合,并定义提升后类型。
如何判断给定类型 T是否属于某个类型的集合?
将类型集合作为模板类型参数包,递归展开该类型包。在递归的每一层判断目标类型 T与当前类型U是否相同,递归返回判断结果的累计或。
template <class T> constexpr bool Any() { returnfalse; } template <class T, class U, class... Types> constexpr bool Any() { return std::is_same<T, U>::value || Any<T, Types...>(); }
如何根据判断结果得到提升后的类型?
用 std::enable_if_t来启用特定的模板,并通过该模板定义提升后的类型。对于default情况,可以类型集合的补集来实现或降低其重载决策等级。
这里采用函数模板来实现(由于 Promotion系列函数模板只出现在decltype表达式中,故不需要定义):
// integer type template <class T> std::enable_if_t<Any<T, bool, short, int, longint, longlongint>(), longlongint> Promotion(int); // float type template <class T> std::enable_if_t<Any<T, float, double, longdouble>(), longdouble> Promotion(int); // default template <class T> T Promotion(...); // sugar template <class T> using type_promotion_t = decltype(Promotion<T>(0));
完整代码(含测试样例):
1 #include <type_traits> 2 3 template <class T> 4 constexpr bool Any() { 5returnfalse; 6} 7 8 template <class T, class U, class... Types> 9 constexpr bool Any() { 10return std::is_same<T, U>::value || Any<T, Types...>(); 11} 1213// integer type14 template <class T> 15 std::enable_if_t<Any<T, bool, short, int, longint, longlongint>(), 16longlongint> Promotion(int); 1718// float type19 template <class T> 20 std::enable_if_t<Any<T, float, double, longdouble>(), 21longdouble> Promotion(int); 2223// default24 template <class T> 25T Promotion(...); 2627// sugar28 template <class T> 29using type_promotion_t = decltype(Promotion<T>(0)); 3031 template <template <class> class Container, class T> 32struct ContainerPromotion { 33using Type = Container<type_promotion_t<T> >; 34}; 3536// Container class template for testing37 template <class T> 38class Vector {}; 3940int main() { 41using Type1 = typename ContainerPromotion<Vector, int *>::Type; 42 static_assert(std::is_same<Type1, Vector<int *> >::value, ""); 4344using Type2 = typename ContainerPromotion<Vector, short>::Type; 45 static_assert(std::is_same<Type2, Vector<longlongint> >::value, ""); 4647using Type3 = typename ContainerPromotion<Vector, float>::Type; 48 static_assert(std::is_same<Type3, Vector<longdouble> >::value, ""); 4950return0; 51 }
FAQ:
-
原文:http://www.cnblogs.com/activa/p/6361465.html
内容总结
以上是互联网集市为您收集整理的C++14 SFINAE 容器类value_type类型提升全部内容,希望文章能够帮你解决C++14 SFINAE 容器类value_type类型提升所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。