#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