当前位置: 首页 > news >正文

泛型vector

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

#include <iostream>
#include <type_traits>
#include <utility>
#include <vector>
#include <stdexcept>
#include <functional>

class Any {

public:

	//notice this!
	template<typename Ty>
	using StorageType = typename std::decay<Ty>::type;

	template<typename Ty, typename =
		typename std::enable_if< !std::is_same<Any, StorageType<Ty>>::value
		&& !std::is_same<Any*, StorageType<Ty>>::value>::type>
		Any(Ty&& value) :container(new Container<StorageType<Ty>>(std::forward<Ty>(value)))
	{
		std::cout<< "constructor" <<std::endl;
	}

	Any(const Any& other)
	{
		if (this->container != nullptr) {
			delete (this->container);
			this->container = other.clone();

		}else {
			this->container = other.clone();
		}
	}

	Any(Any&& other)
	{
		if (this->container != nullptr) {
			delete (this->container);
			this->container = other.container;

		}else {
			this->container = other.container;
		}

		other.container = nullptr;
	}


	Any& operator=(Any&& other)
	{
		if (this->container == nullptr) {
			this->container = other.container;

		}else {
			delete (this->container);
			this->container = other.container;
		}

		other.container = nullptr;
	}

	Any& operator=(const Any& other)
	{
		if (this->container != nullptr) {
			delete (this->container);
			(this->container) = other.clone();

		}else {
			(this->container) = other.clone();
		}
		return *this;
	}

	Any() :container(nullptr) {}

	~Any()
	{
		if (container != nullptr)
		{
			delete container;
			(this->container) = nullptr;
		}
	}

	/*
	template<typename Ty>
	Ty getValue()const noexcept
	{
		if ((this->container) == nullptr) {
			std::runtime_error("There is nothing!");

		}

			Container<StorageType<Ty>>* derived = dynamic_cast<Container<StorageType<Ty>>*>(this->container);

			return (derived->data);
	}*/

	template<typename Ty>
	Ty getValue()
	{
		if ((this->container) == nullptr) {
			std::runtime_error("There is nothing!");

		}
		
		std::cout << "test" <<std::endl;
		Container<Ty>* derived = dynamic_cast<Container<Ty>*>(this->container);
		std::cout << "derived-data: " << derived->data << std::endl;

		return (derived->data);
	}

	/*
	template<typename Ty>
	operator Ty&()
	{

		/*auto*/
		//std::function<Ty& ()const noexcept> function = static_cast<Ty& (Any::*)()const noexcept>(&Any::getValue);

		//Cotainer<StorageType<Ty>>* contain = dynamic_cast<Container<StorageType<Ty>>*>(this->container);

		//return (contain->data);
	//}

	/*template<typename Ty>
	operator Ty()const
	{
		std::function<Ty()noexcept> function = static_cast<Ty(Any::*)()noexcept>(&Any::getValue);
		return (this->*function());
	}*/


private:

	class ContainerBase {
	public:
		ContainerBase() = default;
		ContainerBase(const ContainerBase& ) = default;
		ContainerBase(ContainerBase&& ) = default;
		ContainerBase& operator=(const ContainerBase& )=default;
		ContainerBase& operator=(ContainerBase&& )=default;

		virtual ~ContainerBase() = default;

		virtual ContainerBase* clone()const noexcept = 0; //pure-virtual function.
	};

	template<typename Type>
	class Container : public ContainerBase {
	public:

		Type data;
		
		Container(Type&& value_) :ContainerBase(),data(value_) {}
		Container(const Type& value_) :ContainerBase(),data(value_) {}
		Container(const Container<Type>& other):ContainerBase(other),data(other.data){}
		Container(Container<Type>&& other):ContainerBase(other),data(std::move(other.data)){}
		
		Container& operator=(const Container<Type>& other)
		{
			ContainerBase::operator=(other);
			this->data = other.data;
		}
		
		Container& operator=(Container<Type>&& other)
		{
			ContainerBase::operator=(other);
			this->data = other.data;
		}
		

		virtual ~Container() = default;

		virtual ContainerBase* clone()const noexcept override
		{
			return (new Container<StorageType<Type>>(this->data));
		}
	};

	ContainerBase* clone()const noexcept
	{
		if (this->container == nullptr) {
			return nullptr;

		}else {
			return (this->container)->clone();
		}
	}

	ContainerBase* container;
};

class Eplace_back {
public:
	Eplace_back() = default;
	~Eplace_back() = default;
	Eplace_back(const Eplace_back& other) = delete;
	Eplace_back& operator=(const Eplace_back& other) = delete;

	template<typename Ty>
	void operator()(std::vector<Any>& vec, Ty&& value)
	{
		std::cout << "pus value in vector" << std::endl;
		vec.emplace_back(std::forward<Ty>(value));
	}
};


int main()
{
	std::vector<Any> myVector;
	Eplace_back emplace_back;
	emplace_back(myVector, 100);
	emplace_back(myVector, 200);
	
	std::cout << myVector[0].getValue<int>() << std::endl;
	std::cout << myVector[1].getValue<int>() << std::endl;
	
	std::cout<< "xxxxxxxxxxxx" <<std::endl;

	return 0;

}

 

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/543551

相关文章:

  • [转]SQLServer 2008数据库查看死锁、堵塞的SQL语句
  • Solr5安装部署
  • Scrapy 入门:Hello Scrapy
  • 李嘉诚储藏财富的背后隐藏着什么奥秘?
  • Bitnami Redmine 与 gerrit 整合问题解决
  • 电商项目系列文档(三):秒杀的设计
  • Gitlab数据迁移
  • js模块化开发——require.js学习总结
  • git版本控制实践纪录
  • 用javascript向一个网页连接接口发送请求,并接收该接口返回的json串
  • SpringMVC ------请求参数,请求头,cookie等注解。
  • 机器学习之四:决策树
  • [转载]SharePoint 2013 解决方案中使用JavaScript
  • leetcode第一刷_Merge Sorted Array
  • find详解
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 07.Android之多媒体问题
  • javascript 总结(常用工具类的封装)
  • java小心机(3)| 浅析finalize()
  • Js基础知识(四) - js运行原理与机制
  • JS数组方法汇总
  • npx命令介绍
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • TypeScript迭代器
  • 和 || 运算
  • 聊聊redis的数据结构的应用
  • 深入浅出Node.js
  • 听说你叫Java(二)–Servlet请求
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 原生Ajax
  • 云大使推广中的常见热门问题
  • HanLP分词命名实体提取详解
  • (09)Hive——CTE 公共表达式
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (C语言)二分查找 超详细
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (八)Spring源码解析:Spring MVC
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (学习日记)2024.01.09
  • (循环依赖问题)学习spring的第九天
  • .java 9 找不到符号_java找不到符号
  • /etc/skel 目录作用
  • [04]Web前端进阶—JS伪数组
  • [20170705]lsnrctl status LISTENER_SCAN1
  • [ASP.NET MVC]Ajax与CustomErrors的尴尬
  • [bug总结]: Feign调用GET请求找不到请求体实体类
  • [BZOJ]4817: [Sdoi2017]树点涂色
  • [C#基础]说说lock到底锁谁?
  • [C++ 从入门到精通] 12.重载运算符、赋值运算符重载、析构函数
  • [Codeforces1137D]Cooperative Game
  • [Dxperience.8.*]报表预览控件PrintControl设置
  • [HackMyVM]靶场Crossbow