zoukankan      html  css  js  c++  java
  • 从零开始写STL—模板元编程之any

    any

    class any;
    (since C++17)
    The class any describes a type-safe container for single values of any type.

    • (1) An object of class any stores an instance of any type that satisfies the constructor requirements or is empty, and this is referred to as the state of the class any object. The stored instance is called the contained object. Two states are equivalent if they are either both empty or if both are not empty and if the contained objects are equivalent.
    • (2) The non-member any_cast functions provide type-safe access to the contained object.Implementations are encouraged to avoid dynamic allocations for small objects, but such an optimization may only be applied to types for which std::is_nothrow_move_constructible returns true.

    可以将any看作c++中的Object类(仅仅从功能上看),不过这个类的具体实现是通过模板包装。
    在阅读Muduo网络库时,为每个线程保存的context就是通过any来实现的,STL中提供的泛型粒度太大,而且需要显示指明模板类型(使用类型推断+关键字auto如何?这样我们在存储和取出该变量时又很麻烦),此时使用std::any。

    源码

    • 实现思路
      使用基类来包装模板类隐藏掉泛型(利用any的模板构造函数传入构造),并且提供一组虚函数接口。
    	class holder
    	{
    	public:
    		virtual holder* clone() const = 0;
    		virtual const std::type_info& type() const = 0;
    		virtual ~holder()
    		{
    
    		}
    	};
    

    被包装的模板类存储类型实例,并且实现虚函数接口(克隆,返回类型ID等)

    	template<typename value_type>
    	class dataholder : public holder
    	{
    	private:
    		typedef dataholder<value_type> self;
    	public:
    		dataholder(const value_type& v) :val(v) {}
    		dataholder(const self&) = delete;
    		self& operator = (const self& rhs) = delete;
    
    		virtual dataholder* clone() const
    		{
    			return new dataholder(val);
    		}
    
    		virtual const std::type_info& type() const
    		{
    			return typeid(val);
    		}
    		value_type val;
    	};
    
    • any 的实现:
      利用基类指针+ 模板 接受不同lei'xin
      利用强制类型转换来向下转型(使用typeid实现类型安全)
    	class any
    	{
    	public:
    		template<typename value_type>
    		friend value_type& any_cast(const any& rhs);
    		any() :content(nullptr) {}
    		template<typename value_type>
    		any(const value_type& val) :content(new dataholder<value_type>(val)) {}
    		any(const any& rhs)
    		{
    			content = rhs.content->clone();
    		}
    
    		any& operator=(const any& rhs)
    		{
    			any tmp(rhs);
    			std::swap(*this, tmp);
    		}
    		
    		~any()
    		{
    			delete content;
    		}
    
    		const std::type_info& type() const
    		{
    			return content == nullptr ? typeid(void) : content->type();
    		}
    	private:
    		holder* content;
    	};
    
    	template<typename value_type>
    	value_type& any_cast(const any& rhs)
    	{
    		assert(typeid(typename value_type) == rhs.type());
    		return static_cast<dataholder<value_type>*>(rhs.content)->val;
    	}
    
    
  • 相关阅读:
    redis基础配置
    brew安装mysql
    iptables 执行清除命令 iptables -F 要非常小心
    nginx反向代理部署nodejs配置
    Starting MySQL... ERROR! The server quit without updating PID file 问题解决
    iframe自适应高度问题
    js正则常用的一些东西
    node.js批量重命名文件
    [转]MySQL5字符集支持及编码研究
    PHP $_SERVER的使用
  • 原文地址:https://www.cnblogs.com/joeylee97/p/8877248.html
Copyright © 2011-2022 走看看