How do boost::variant and boost::any work?

后端 未结 3 1678
孤城傲影
孤城傲影 2020-12-02 06:40

How do variant and any from the boost library work internally? In a project I am working on, I currently use a tagged union. I want to use something else, because unions in

3条回答
  •  無奈伤痛
    2020-12-02 06:52

    If you read the boost::any documentation they provide the source for the idea: http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf

    It's basic information hiding, an essential C++ skill to have. Learn it!

    Since the highest voted answer here is totally incorrect, and I have my doubts that people will actually go look at the source to verify that fact, here's a basic implementation of an any like interface that will wrap any type with an f() function and allow it to be called:

    struct f_any
    {
       f_any() : ptr() {}
       ~f_any() { delete ptr; }
       bool valid() const { return ptr != 0; }
       void f() { assert(ptr); ptr->f(); }
    
       struct placeholder
       {
         virtual ~placeholder() {}
         virtual void f() const = 0;
       };
    
       template < typename T >
       struct impl : placeholder
       {
         impl(T const& t) : val(t) {}
         void f() const { val.f(); }
         T val;
        };
       // ptr can now point to the entire family of 
       // struct types generated from impl
       placeholder * ptr;
    
       template < typename T >
       f_any(T const& t) : ptr(new impl(t)) {}
    
      // assignment, etc...
    };
    

    boost::any does the same basic thing except that f() actually returns typeinfo const& and provides other information access to the any_cast function to work.

提交回复
热议问题