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;
    	}
    
    
  • 相关阅读:
    AndroidStudio打开新项目后解决下载某版本gradle慢的问题
    GeoServer怎样修改线性地图的颜色样式
    GeoServer简介、下载、配置启动、发布shapefile全流程(图文实践)
    AndroidStudio中打开新项目提示:This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot o
    若依微服务版在Windows上通过jar包运行业务模块时提示:Failed to determine s suitable driver class
    若依微服务版后台服务通过jar包部署到Windows服务器
    腾讯云centos7安装MySQL
    使用Navicat for MySQL把本地数据库上传到服务器
    浏览器页面乱码
    事务的配置
  • 原文地址:https://www.cnblogs.com/joeylee97/p/8877248.html
Copyright © 2011-2022 走看看