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;
}

最新文章

  1. 基于C/S架构的3D对战网络游戏C++框架 _06搭建C/S架构的基本通信框架(尚未写完会重新编辑后再发出)
  2. Android IPC机制之ContentProvider
  3. MS CRM商机产品等Mapping
  4. 详解应对平台高并发的分布式调度框架TBSchedule
  5. Unix网络编程 -- ubuntu下搭建编译环境( 解决unp.h 编译等问题)
  6. linux下分卷tar.bz文件的合并并解压缩
  7. xdebug
  8. 使用命令行工具将Android应用转换成BlackBerry PlayBook应用
  9. 【Maven实战】仓库介绍和Nexus的安装
  10. Git冲突解决方案
  11. 用 config drive 配置网络 - 每天5分钟玩转 OpenStack(173)
  12. [转载] 使用 Twitter Storm 处理实时的大数据
  13. Python3简单的输入输出及内置函数查看
  14. IBM优质资源
  15. DxPackNet 5.视频高质量的压缩和传输
  16. selenium之元素定位-xpath
  17. MySQL replace into (insert into 的增强版)
  18. Scyther spdl(比较准确的翻译)
  19. NOIP-珠心算
  20. 如何阅读jdk源码?

热门文章

  1. vue利用计算属性做(展开收起)小例子
  2. LINUX 安装tsung 对OPENFIRE 进行压力测试
  3. IE11/Flash页游白屏怎么办!立刻开启IE大地址模式!缓解浏览器白屏问题
  4. VINS-Fusion代码阅读(四)
  5. vue之placeholder中引用字体图标
  6. 检查bug
  7. zabbix4.2学习笔记系列
  8. Swift 中的Range和NSRange不同
  9. 安装Yii2提示Failed to decode response: zlib_decode(): data error错误解决方法
  10. 解决CSDN阅读全部需要登录的问题