Boost::any Learning

Source: Internet
Author: User

#include <iostream> #include <typeinfo> #include <list>using namespace Std;class any{public:// Interface class Placeholder {public://Virtual ~placeholder () {}//Virtual const ST        d::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 () CO        NST {return typeid (ValueType);        }//Virtual Placeholder * Clone () const {return new holder (held);    } public://representation ValueType held; };p Ublic:any (): content (0) {} template<typename valuetype> any (const ValueType & value): Conten T (new holder<valuetype> (value)) {}//copy constructor any (const all & 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 (n    EW holder< typename Decay<valuetype>::type > (static_cast<valuetype&&> (Value))) {}//    ~any () {delete content;        }//any & swaps (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::b Ad_any_cast: "//" failed conversion using boost::any_cast ";//}};template<typename Valuetype>value Type * 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& gt; (Const_cast<any *> (operand));} Template<typename valuetype>inline ValueType any_cast (const any & operand) {Const VALUETYPE * result = Any_ca    St<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 type"));    Const CHAR* p = "Const char* string";    List.push_back (P); for (const auto& elem:list) {if (elem.type () = = typeid (int)) Std::cout<<any_cast<int&gt        ;(elem) <<std::endl; else if (elem.type () = = typeID (std::string)) std::cout<<any_cast<std::string> (elem) <<std::en        dl else if (elem.type () = = typeid (const char*)) std::cout<<any_cast<Const char*> (elem) <<std::endl; } return 0;}


Boost::any Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.