visitor pattern for boost::any

前端 未结 2 461
盖世英雄少女心
盖世英雄少女心 2021-01-05 22:41

I found this https://gist.github.com/2945472 but I need an implementation that does not depend on c++11. I tried my hand at converting it to use only boost, but I\'m having

2条回答
  •  执笔经年
    2021-01-05 23:24

    Try to use extendable any https://sourceforge.net/projects/extendableany/?source=directory.

    struct f_method
    {
        typedef void (boost::mpl::_1::* signature) () const;
    
        template 
        struct wrapper
            : public T
        {
            void f() const
            {
                return this->call(f_method());
            }
        };
    
        struct implementation
        {
            void operator() (int i) const
            {
               std::cout << "fa(" << i << ")" << std::endl;
            }
    
            void operator() (abc) const
            {
               std::cout << "fb(abc())" << std::endl;
            }
    
            template 
            void operator() (const T& t) const
            {
                std::cout << "Errr" << std::endl;
            }
        };
    };
    
    typedef xany > any;
    
    int main() {
        std::vector xs;
        xs.push_back(1);
        xs.push_back(abc());
        xs.push_back(1.5);
    
        for (auto it=xs.begin(); it!=xs.end(); ++it)
            (*it).f();
    }
    

提交回复
热议问题