#pragma once
#include <memory>
#include <typeindex>
#include <stdexcept>
#include <iostream>
struct Any
{
Any(void) : m_tpIndex(std::type_index(typeid(void))) {}
Any(const Any& that) : m_ptr(that.Clone()), m_tpIndex(that.m_tpIndex) {}
Any(Any && that) : m_ptr(std::move(that.m_ptr)), m_tpIndex(that.m_tpIndex) {}
template<typename U, class = typename std::enable_if<!std::is_same<typename std::decay<U>::type, Any>::value, U>::type> Any(U && value) :
m_ptr(new Derived < typename std::decay<U>::type>(std::forward<U>(value))),
m_tpIndex(std::type_index(typeid(typename std::decay<U>::type))){}
bool IsNull() const { return !bool(m_ptr); }
template<class U> bool Is() const
{
return m_tpIndex == std::type_index(typeid(U));
}
template<class U>
U& AnyCast()
{
if (!Is<U>())
{
std::cout << "can not cast " <<
C++11实现Boost Any
最新推荐文章于 2019-08-29 21:05:37 发布