栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++:SGI STL——

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++:SGI STL——

文章目录
  • type_traits概念
  • 代码
  • END

type_traits概念

iterator_traits负责萃取迭代器的特性, __type_traits负责萃取型别(type)的特性。
此处我们所关注的型别特性是指:
这个型别是否具备non-trivial defalt ctor?
是否具备non-trivial copy ctor?
是否具备non-trivial assignmentoperator?
是否具备non-trivial dtor?
如果答案是否定的,我们在对这个型别进行构造、析构、拷贝、赋值等操作时,就可以采用最有效率的措施(例如根本不调用身居高位,不谋实事的那些constructor,destructor ),而采用内存直接处理操作如malloc ( )、memcpy ()等等,获得最高效率。这对于大规模而操作频繁的容器,有着显著的效率提升。

根据iterator_traits得来的经验,我们希望,程序之中可以这样运用__type_traits,T代表任意型别:

__type_traits::has_trivial_default_constructor
__type_traits::has_trivial_copy_constructor
__type_traits::has_trivial_assignment_operator
__type_traits::has_trivial_destructor
__type_traits::is_POD_type // POD : Plain Old Data

我们希望上述式子响应我们“真”或“假”(以便我们决定采取什么策略) 但其结果不应该只是个bool 值,应该是个有着真/假性质的“对象”,因为我们希望利用其响应结果来进行参数推导,而编译器只有面对class object形式的参数才会做参数推导。为此,上述式子应该传回这样的东西:

struct __true_type { };
struct __false_type { };

这两个空白 classes没有任何成员,不会带来额外负担,却又能够标示真假,满足我们所需。

为了达成上述五个式子,__type_traits内必须定义一些typedefs,其值不是_true_type 就是 __false_type。下面是SGI 的做法:

template 
struct __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;
};

上述__type_traits 可以接受任何型别的参数,五个typedefs将经由以下管道获得实值:

  1. 一般具现体( general instantiation),内含对所有型别都必定有效的保守值。上述各个has_trivial_xxx型别都被定义为__false_type,就是对所有型别都必定有效的保守值。
  2. 经过声明的特化版本,例如内对所有C++标量型别( scalar types)提供了对应的特化声明。
  3. 某些编译器(如Silicon Graphics N32和 N64编译器)会自动为所有型别提供适当的特化版本。
代码

对内置类型提供特化版本

#ifndef MY_TYPE_TRAITS_H
#define MY_TYPE_TRAITS_H

namespace Srh
{
	struct __true_type {};
	struct __false_type {};

	template 
	struct __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; 
	};

	// 特化版本
	template<> struct __type_traits
	{
		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;  
	};

	template<> struct __type_traits
	{
		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;   
	};

	template<> struct __type_traits
	{
		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;  
	};

	template<> struct __type_traits
	{
		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;   
	};

	template<> struct __type_traits
	{
		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;   
	};
	template<> struct __type_traits
	{
		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;   
	};
	template<> struct __type_traits
	{
		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;   
	};
	template<> struct __type_traits
	{
		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;   
	};
	template<> struct __type_traits
	{
		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;   
	};

	template<> struct __type_traits
	{
		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;   
	};
	template<> struct __type_traits
	{
		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;   
	};
	template<> struct __type_traits
	{
		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;   
	};

	template<> struct __type_traits
	{
		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;   
	};

	template<> struct __type_traits
	{
		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;   
	};
	template
	struct __type_traits
	{
		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
END

参考自《STL源码剖析》

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846723.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号