// Filename: type_traits.h// Comment By: 凝霜// E-mail: mdl2009@vip.qq.com// Blog: http://blog.csdn.net/mdl13412/* * * Copyright (c) 1997 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Silicon Graphics makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. */#ifndef __TYPE_TRAITS_H#define __TYPE_TRAITS_H#ifndef __STL_CONFIG_H#include <stl_config.h>#endif// 這個標頭檔提供了一個架構, 允許編譯器在編譯期根據類型屬性派發函數的機制// 這個架構對於編寫模板代碼非常有用// 例如, 當我們需要拷貝一個未知類型的array, 它能協助我們得知這個未知類型// 是否具有一個trivial copy constructor, 以協助我們決定memcpy()是否能使用// __type_traits模板類提供了一些列的typedefs, 其值是__true_type或者__false_type// __type_traits模板類可以接受任何類型的參數// 模板類中的typedefs可以通過以下手段擷取其正確值// 1. general instantiation, 對於所有類型都要有保守值// 2. 經過特化的版本// 3. 一些編譯器(例如Silicon Graphics N32 and N64 compilers)// 會自動給所有類型提供合適的特化版本//// 例子:// Copy an array of elements which have non-trivial copy constructors// template <class T> void copy(T* source,T* destination,int n,__false_type);// Copy an array of elements which have trivial copy constructors. Use memcpy.// template <class T> void copy(T* source,T* destination,int n,__true_type);//// Copy an array of any type by using the most efficient copy mechanism// template <class T> inline void copy(T* source,T* destination,int n)// {// copy(source,destination,n,// typename __type_traits<T>::has_trivial_copy_constructor());// }struct __true_type{};struct __false_type{};template <class type>struct __type_traits{ // 不要移除這個成員 // 它通知能自動特化__type_traits的編譯器, 現在這個__type_traits template是特化的 // 這是為了確保萬一編譯器使用了__type_traits而與此處無任何關聯的模板時 // 一切也能順利運作 typedef __true_type this_dummy_member_must_be_first; // 以下條款應當被遵守, 因為編譯器有可能自動組建類型的特化版本 // - 你可以重新安排的成員次序 // - 你可以移除你想移除的成員 // - 一定不可以修改下列成員名稱, 卻沒有修改編譯器中的相應名稱 // - 新加入的成員被當作一般成員, 除非編譯器提供特殊支援 typedef __false_type has_trivial_default_constructor; typedef __false_type has_trivial_copy_constructor; typedef __false_type has_trivial_assignment_operator; typedef __false_type has_trivial_destructor; typedef __false_type is_POD_type;};// 以下針對C++內建的基礎資料型別 (Elementary Data Type)提供特化版本, 使其具有trivial default constructor,// copy constructor, assignment operator, destructor// 並標記其為POD類型//// 特化類型:// char, signed char, unsigned char,// short, unsigned short// int, unsigned int// long, unsigned long// float, double, long double// Provide some specializations. This is harmless for compilers that// have built-in __types_traits support, and essential for compilers// that don't.__STL_TEMPLATE_NULL struct __type_traits<char>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<signed char>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<unsigned char>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<short>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<unsigned short>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<int>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<unsigned int>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<long>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<unsigned long>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<float>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<double>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};__STL_TEMPLATE_NULL struct __type_traits<long double>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION// 針對指標提供特化template <class T>struct __type_traits<T*>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */// 針對char *, signed char *, unsigned char *提供特化struct __type_traits<char*>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};struct __type_traits<signed char*>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};struct __type_traits<unsigned char*>{ typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type;};#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */#endif /* __TYPE_TRAITS_H */// Local Variables:// mode:C++// End: