C++11 type_traits 之is_same源码分析
2014-11-28 15:23
232 查看
请看源码:
1. is_same是模版,integral_constant也是模版
2. true_type和false_type是integral_constant实例化的类型
3.is_same通用的模版继承了false_type
4.is_same的偏特化模版继承了true_type
5.is_same模版实参类型一致时,会实例化偏特化模版,而偏特化模版继承了true_type
6.integral_constant<bool, true> 即truetype的value是true
is_same和static_assert配合使用,可以在编译期进行强大的类型检查。使用例子如下:
static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");静态断言,当用户使用不支持类型时,立即阻止用户编译。
template<typename _Tp, _Tp __v> struct integral_constant { static const _Tp value = __v; typedef _Tp value_type; typedef integral_constant<_Tp, __v> type; }; /// typedef for true_type typedef integral_constant<bool, true> true_type; /// typedef for false_type typedef integral_constant<bool, false> false_type; template<typename, typename> struct is_same : public false_type { }; template<typename _Tp> struct is_same<_Tp, _Tp> : public true_type { };
1. is_same是模版,integral_constant也是模版
2. true_type和false_type是integral_constant实例化的类型
3.is_same通用的模版继承了false_type
4.is_same的偏特化模版继承了true_type
5.is_same模版实参类型一致时,会实例化偏特化模版,而偏特化模版继承了true_type
6.integral_constant<bool, true> 即truetype的value是true
is_same和static_assert配合使用,可以在编译期进行强大的类型检查。使用例子如下:
struct A{~A(){cout<<"delete A..."<<endl;}}; template<typename T> struct TypeTraits { typedef void TYPE; }; template<> struct TypeTraits<std::string> { typedef std::string TYPE; }; template<> struct TypeTraits<long> { typedef long TYPE; }; template<> struct TypeTraits<A> { typedef A TYPE; }; template<> struct TypeTraits<double> { typedef double TYPE; }; class DeleteLong { public: void operator()(void *p) { delete static_cast<long*>(p); } }; class DeleteString { public: void operator()(void *p) { delete static_cast<string*>(p); } }; class DeleteDouble { public: void operator()(void *p) { delete static_cast<double*>(p); } }; class DeleteA { public: void operator()(void *p) { delete static_cast<A*>(p); } }; class ExportData { void* vp; enum my_type {SP,LP,DP,AP} types; static unordered_map<type_index,my_type> typeMap; static vector<function<void(void*)>> deleters; public: template <typename T> ExportData(const T& t) { static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!"); vp=new T(t); types= typeMap[typeid(T)]; } template <typename T> void setData(const T& t) { static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!"); assert(types==typeMap[typeid(T)]); *(static_cast<T*>(vp))=t; } template <typename T> void getData(T& t) { static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!"); assert(types==typeMap[typeid(T)]); t=*(static_cast<T*>(vp)); } ~ExportData() { (deleters[types])(vp); } }; unordered_map<type_index,ExportData::my_type> ExportData::typeMap { {typeid(string),ExportData::my_type::SP}, {typeid(long),ExportData::my_type::LP}, {typeid(double),ExportData::my_type::DP}, {typeid(A),ExportData::my_type::AP} }; vector<function<void(void*)>> ExportData::deleters {DeleteString(),DeleteLong(),DeleteDouble(),DeleteA()};
static_assert(is_same<typename TypeTraits<T>::TYPE,T>::value,"not support!");静态断言,当用户使用不支持类型时,立即阻止用户编译。
相关文章推荐
- C++11 type_traits 之is_convertible源码分析
- C++11 type_traits 之is_pointer,is_member_function_pointer源码分析
- c++11——type_traits 类型萃取
- 【c++11】static_assert: 静态断言 type traits:类型特征
- C++11标准库之Type Traits简介
- 模版元编程:C++11中type traits的部分实现
- C++11 利用const_cast和type_traits修改类成员常量的通用模板函数
- 【C++11学习笔记】类型判断的type_traits学习
- java异常-The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly reference
- Access restriction: The type SunJCE is not accessible due to restriction on required
- Uncaught TypeError: sum is not a function
- CodeSign error: code signing is required for product type Application in SDK iOS解决办法
- Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not access
- C++11实现for each(type var in collection)能识别的类
- Element 'property' cannot have character [children], because the type's content type is element-only
- [转] EF cannot be tracked because another instance of this type with the same key is already being tracked
- PCL 中 pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr 出现incomplete type is not allowed 的解决方法
- error Type referred to is not an annotation type: validate 0 formal unbound in pointcut
- 项目报错:The method getTextContent() is undefined for the type Node
- TypeError: elem.nodeName is undefined