Partially specialize methods of variadic template classes

后端 未结 2 841
一整个雨季
一整个雨季 2021-01-25 23:54

I want to write an unmarshaller to extract arguments stored in a msgpack array for individual arguments to a call of sigc::signal::emit(...). I tried this:

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-26 00:21

    I have always felt that the most maintainable way to specialise a member function is to defer to a specialised function object:

    #include 
    
    struct object_array
    {
    };
    
    
    template
    struct implement_do_emit
    {
        template
        void operator ()(This *that, const object_array& arg) const;
    };
    
    
    template
    struct MsgpackAdapter
    {
        friend class implement_do_emit;
    
        virtual void do_emit(const object_array& mp_args)
        {
            auto impl = implement_do_emit();
            impl(this, mp_args);
        }
    
    };
    
    template
    struct implement_do_emit
    {
        template
        void operator ()(This *that, const object_array& arg) const
        {
            std::cout << "one type version\n";
        }
    };
    
    template
    struct implement_do_emit
    {
        template
        void operator ()(This *that, const object_array& arg) const
        {
            std::cout << "two type version\n";
        }
    };
    
    
    int main()
    {
        MsgpackAdapter pi {};
        pi.do_emit(object_array());
    
        MsgpackAdapter pii {};
        pii.do_emit(object_array());
    }
    

提交回复
热议问题