2017-01-02 252 views
0

考虑以下简化的变体类代码片段。它大部分是用于信息目的,问题是关于conditional_invoke方法。带多个参数包的C++方法

// Possible types in variant. 
enum class variant_type { empty, int32, string }; 

// Actual data store. 
union variant_data { 
    std::int32_t val_int32; 
    std::string val_string; 
    inline variant_data(void) { /* Leave uninitialised */ } 
    inline ~variant_data(void) { /* Let variant do clean up. */ } 
}; 

// Type traits which allow inferring which type to use (these are actually generated by a macro). 
template<variant_type T> struct variant_type_traits { }; 
template<class T> struct variant_reverse_traits { }; 

template<> struct variant_type_traits<variant_type::int32> { 
    typedef std::int32_t type; 
    inline static type *get(variant_data& d) { return &d.val_int32; } 
}; 

template<> struct variant_reverse_traits<std::int32_t> { 
    static const variant_type type = variant_type::int32; 
    inline static std::int32_t *get(variant_data& d) { return &d.val_int32; } 
}; 

template<> struct variant_type_traits<variant_type::string> { 
    typedef std::string type; 
    inline static type *get(variant_data& d) { return &d.val_string; } 
}; 

template<> struct variant_reverse_traits<std::string> { 
    static const variant_type type = variant_type::string; 
    inline static std::string *get(variant_data& d) { return &d.val_string; } 
}; 

// The actual variant class. 
class variant { 
public: 

    inline variant(void) : type(variant_type::empty) { } 

    inline ~variant(void) { 
     this->conditional_invoke<destruct>(); 
    } 

    template<class T> inline variant(const T value) : type(variant_type::empty) { 
     this->set<T>(value); 
    } 

    template<class T> void set(const T& value) { 
     this->conditional_invoke<destruct>(); 
     std::cout << "Calling data constructor ..." << std::endl; 
     ::new (variant_reverse_traits<T>::get(this->data)) T(value); 
     this->type = variant_reverse_traits<T>::type; 
    } 

    variant_data data; 
    variant_type type; 

    private: 

    template<variant_type T> struct destruct { 
     typedef typename variant_type_traits<T>::type type; 
     static void invoke(type& v) { 
      std::cout << "Calling data destructor ..." << std::endl; 
      v.~type(); 
     } 
    }; 

    template<template<variant_type> class F, class... P> 
    inline void conditional_invoke(P&&... params) { 
     this->conditional_invoke0<F, variant_type::int32, variant_type::string, P...>(std::forward<P>(params)...); 
    } 

    template<template<variant_type> class F, variant_type T, variant_type... U, class... P> 
    void conditional_invoke0(P&&... params) { 
     if (this->type == T) { 
      F<T>::invoke(*variant_type_traits<T>::get(this->data), std::forward<P>(params)...); 
     } 
     this->conditional_invoke0<F, U..., P...>(std::forward<P>(params)...); 
    } 

    template<template<variant_type> class F, class... P> 
    inline void conditional_invoke0(P&&... params) { } 
}; 

的代码以这种方式工作,即,它只要在仿函数的参数列表P...是空的作品。如果我添加其他仿像

template<variant_type T> struct print { 
    typedef typename variant_type_traits<T>::type type; 
    static void invoke(type& v, std::ostream& stream) { 
     stream << v; 
    } 
}; 

,并尝试调用它

friend inline std::ostream& operator <<(std::ostream& lhs, variant& rhs) { 
    rhs.conditional_invoke<print>(lhs); 
    return lhs; 
} 

编译器VS 20115投诉

error C2672: 'variant::conditional_invoke0': no matching overloaded function found

或GCC分别

error: no matching function for call to 'variant::conditional_invoke0 >&>(std::basic_ostream&)'

我猜编译器不能deci当U...结束并且当P...开始时。有什么办法可以解决这个问题吗?

+0

这将需要一些大的变化,但我会抛弃'variant_type ...'和使用元组来代替。这样,你可以使用'template