type_traits.h

type_traits.h
// Filename:    type_traits.h

// Comment By:  凝霜
// E-mail:      [email protected]
// 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++内置的基本数据类型提供特化版本, 使其具有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:
时间: 2024-08-29 10:35:16

type_traits.h的相关文章

stl_algobase.h

stl_algobase.h // Filename: stl_algobase.h // Comment By: 凝霜 // E-mail: [email protected] // Blog: http://blog.csdn.net/mdl13412 // 这个文件中定义的都是一些最常用的算法, 我仅仅给出一个思路, // 不进行详尽讲解, 具体算法请参考算法书籍, 推荐<算法导论> // 另外, 对于基础薄弱的, 推荐<大话数据结构>, 此书我读了一下 // 试读章节, 适

STL源码剖析——基本算法stl_algobase.h

前言 在STL中,算法是经常被使用的,算法在整个STL中起到非常重要的作用.本节介绍的是一些基本算法,包含equal,fill,fill_n,iter_swap,lexicographical_compare,max,min,mismatch,swap,copy,copy_backward,copy_n.其中一个比较重要的算法就是copy,针对copy的剖析在源码中可以看到详细的注解.本文剖析的源码出自SGL STL中的<stl_algobase.h>文件. 基本算法剖析 #ifndef __

《STL源码剖析》---stl_algobase.h阅读笔记

STL标准中没有区分基本算法或复杂算法,单SGI把常用的一些算法定义在<stl_algobase.h>只中,其他算法定义在<stl_algo.h>中. stl_algobase.h中的算法,比较值得学习的是copy(),它"无所不用其极"的改善效率.copy的目的是复制一段元素到指定区间,复制操作最容易想到赋值操作符=,但是有的赋值操作符=是trivial的,可以直接拷贝.关于赋值操作符=是不是trivial的,可以参考"Memberwise copy

STL源代码剖析——基本算法stl_algobase.h

前言 在STL中.算法是常常被使用的,算法在整个STL中起到很关键的数据.本节介绍的是一些基本算法,包括equal.fill.fill_n,iter_swap.lexicographical_compare.max,min.mismatch,swap,copy,copy_backward.copy_n.当中一个比較重要的算法就是copy.针对copy的剖析在源代码中能够看到具体的注解. 本文剖析的源代码出自SGL STL中的<stl_algobase.h>文件. 基本算法剖析 #ifndef

stl源码剖析学习笔记(二)traits编程技法简明例程

解释说明 traits侯捷老师的翻译是萃取.其目的就是在编译期进行模板调用的类型识别,从而做一些事情. 最突出的例子,我觉得不是<STL源码剖析>中"迭代器概念与traits编程技法"这一章的说明,而是stl算法中copy的实现.代码在stl源码的stl_algobase.h中. copy的最终实现,大致分为两类,一类是直接整块内存的memmove操作,另一类是一个个对象赋值.其中涉及has_trivial_assignment_operator的类型推断. 如果has_t

在VC6中使用ogre进行游戏开发

微软公司开发的visual c++6.0堪称史上最易用.最成熟的开发工具.vc6以其小巧.轻便赢得了程序员的喜爱,以至于在VS大行其道的时代,很多程序员仍然使用vc6作为开发工具,vc6的欢迎性可见一斑.可惜vc6有一个缺点,那就是不能开发ogre应用程序.网上的教程,链接已经过时.个人也是查找了好多资料,搞了整整一上午才终于配置得差不多,但仍然失败了好几个,不过已经不影响大局了.ogre是一个极其复杂的工程,它借助了许多的第三方库,比如说boost.stlport.所以编译ogre也是一个相当

Google proto buffer的安装/使用

protobuf安装/使用原本是要在官网上下载的:http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz可惜已被墙,幸好有好心人提供了以下地址:http://pan.baidu.com/s/1pJlZubT 为了说明安装过程中文件的作用,我就指定目录安装了: ./configure --prefix=/usr/local/protobuf/ make make check make install 当然,安装前需要确保自己安装了gcc

STL Traits编程技法

traits编程技法大量运用于STL实现中.通过它在一定程度上弥补了C++不是强型别语言的遗憾,增强了C++关于型别认证方面的能力. traits编程技法是利用"内嵌型别"的编程技法和编译器的template参数推导功能实现的. iterator_traits 1.对于class type要求其"内嵌型别" 要求与STL兼容的容器,其迭代器必须定义一下五种型别: iterator_category 迭代器类型 value_type 迭代器所指对象类型 differe

STL的框架实现

1.模板的特化 泛化模板 : 对各种不同版本的特殊化处理---->特化; 2.萃取 拿相同的代码提取不同的类型; 3.整个搭建框架如下: 在这里已经不关心空间配置器是如何实现的; 其框架代码如下: #ifndef _MEMORY_H_ #define _MEMORY_H_ #include"stl_alloc.h" #include"stl_iterator.h" #include"type_traits.h" #include"