标签:
#include <iostream> #include <typeinfo> #include <list> using namespace std; class any { public: //interface class placeholder { public: // virtual ~placeholder(){} // virtual const std::type_info& type() const = 0; // virtual placeholder * clone() const = 0; }; template<typename ValueType> class holder : public placeholder { public: // structors holder(const ValueType & value) : held(value) { } // virtual const std::type_info& type() const { return typeid(ValueType); } // virtual placeholder * clone() const { return new holder(held); } public: // representation ValueType held; }; public: any(): content(0) { } template<typename ValueType> any(const ValueType & value): content(new holder<ValueType>(value)) { } //copy constructor any(const any & other): content(other.content ? other.content->clone() : 0) { } // Move constructor any(any&& other) : content(other.content) { other.content = 0; } // Perfect forwarding of ValueType template<typename ValueType> any(ValueType&& value): content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value))) { } // ~any() { delete content; } // any & swap(any & rhs) { std::swap(content, rhs.content); return *this; } // any & operator=(const any& rhs) { any(rhs).swap(*this); return *this; } // move assignement any & operator=(any&& rhs) { rhs.swap(*this); any().swap(rhs); return *this; } // Perfect forwarding of ValueType template <class ValueType> any & operator=(ValueType&& rhs) { any(static_cast<ValueType&&>(rhs)).swap(*this); return *this; } // bool empty() const { return !content; } // void clear() { any().swap(*this); } //。 const std::type_info& type() const { return content ? content->type() : typeid(void); } private: // representation template<typename ValueType> friend ValueType * any_cast(any *) ; template<typename ValueType> friend ValueType * unsafe_any_cast(any *) ; placeholder * content; }; inline void swap(any & lhs, any & rhs) { lhs.swap(rhs); } class bad_any_cast : public std::bad_cast { public: // virtual const char * what() const // { // return "boost::bad_any_cast: " // "failed conversion using boost::any_cast"; // } }; template<typename ValueType> ValueType * any_cast(any * operand) { return operand && operand->type() == typeid(ValueType)? &static_cast<any::holder<ValueType>*>(operand->content)->held: 0; } template<typename ValueType> inline const ValueType * any_cast(const any * operand) { return any_cast<ValueType>(const_cast<any *>(operand)); } template<typename ValueType> inline ValueType any_cast(const any & operand) { const ValueType * result = any_cast<ValueType>(&operand); if(!result) throw bad_any_cast(); return *result; } int main() { typedef std::list<any> list_any; list_any list; list.push_back(10); list.push_back( std::string("std::string 类型") ); const char* p = "const char* 字符串"; list.push_back(p); for(const auto& elem:list) { if( elem.type() == typeid(int) ) std::cout<<any_cast<int>(elem)<<std::endl; else if( elem.type() == typeid(std::string) ) std::cout<<any_cast<std::string>(elem)<<std::endl; else if( elem.type() == typeid(const char*) ) std::cout<<any_cast<const char*>(elem)<<std::endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/code_my_life/article/details/51333445