博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
泛型vector
阅读量:6511 次
发布时间:2019-06-24

本文共 3855 字,大约阅读时间需要 12 分钟。

hot3.png

#include 
#include
#include
#include
#include
#include
class Any {public: //notice this! template
using StorageType = typename std::decay
::type; template
>::value && !std::is_same
>::value>::type> Any(Ty&& value) :container(new Container
>(std::forward
(value))) { std::cout<< "constructor" <
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
Ty getValue()const noexcept { if ((this->container) == nullptr) { std::runtime_error("There is nothing!"); } Container
>* derived = dynamic_cast
>*>(this->container); return (derived->data); }*/ template
Ty getValue() { if ((this->container) == nullptr) { std::runtime_error("There is nothing!"); } std::cout << "test" <
* derived = dynamic_cast
*>(this->container); std::cout << "derived-data: " << derived->data << std::endl; return (derived->data); } /* template
operator Ty&() { /*auto*/ //std::function
function = static_cast
(&Any::getValue); //Cotainer
>* contain = dynamic_cast
>*>(this->container); //return (contain->data); //} /*template
operator Ty()const { std::function
function = static_cast
(&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
class Container : public ContainerBase { public: Type data; Container(Type&& value_) :ContainerBase(),data(value_) {} Container(const Type& value_) :ContainerBase(),data(value_) {} Container(const Container
& other):ContainerBase(other),data(other.data){} Container(Container
&& other):ContainerBase(other),data(std::move(other.data)){} Container& operator=(const Container
& other) { ContainerBase::operator=(other); this->data = other.data; } Container& operator=(Container
&& other) { ContainerBase::operator=(other); this->data = other.data; } virtual ~Container() = default; virtual ContainerBase* clone()const noexcept override { return (new Container
>(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
void operator()(std::vector
& vec, Ty&& value) { std::cout << "pus value in vector" << std::endl; vec.emplace_back(std::forward
(value)); }};int main(){ std::vector
myVector; Eplace_back emplace_back; emplace_back(myVector, 100); emplace_back(myVector, 200); std::cout << myVector[0].getValue
() << std::endl; std::cout << myVector[1].getValue
() << std::endl; std::cout<< "xxxxxxxxxxxx" <

 

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

你可能感兴趣的文章
mvp和rxjava继续学习
查看>>
openjdk 打包编译问题
查看>>
有用的HTML5 pattern属性
查看>>
听说你想 520 表白
查看>>
使用iText JAR生成PDF
查看>>
Delphi 中的 procedure of object
查看>>
数据结构教案
查看>>
Android App的签名打包(晋级篇)
查看>>
php 源码安装参数配置大全
查看>>
android mediaplayer 架构
查看>>
@的用法
查看>>
session多服务器共享的方案梳理
查看>>
开源 免费 java CMS - FreeCMS1.7 网上调查
查看>>
设计模式02-职责链模式
查看>>
webstorm的体验
查看>>
httpclient 4.3.3 Timeout waiting for connection
查看>>
system_ex():增强版别的system()
查看>>
thinkphp改写的上传类及缩放水印类
查看>>
01-UI基础-02UILable
查看>>
12323
查看>>