带多个参数包的C++方法

问题描述:

考虑以下简化的变体类代码片段。它大部分是用于信息目的,问题是关于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

+2

'template struct variant_type_list {};''是你需要的,([** demo **](http://coliru.stacked-crooked.com/a/7083d534b3f19b5e)) –

+0

谢谢你的建议。如果我理解正确,这个想法是将其中一个包分组到一个单独的结构中,这个结构可以作为一个单独的类型传递,只剩下'P ...'作为唯一可变参数。我不明白的atm是方法的签名必须看起来像我可以从'conditional_invoke0'中的'VAR_TYPES' /'variant_type_list'中检索当前类型。 – Christoph

您必须制作两个参数包。也就是说,让type和non-type模板参数成为函数参数列表的一部分。为此,引入一个伪结构:

template <variant_type...> 
struct variant_type_list {}; 

,让编译器从一个函数调用推断出variant_type...包:

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

    this->conditional_invoke0<F>(variant_type_list<U...>{} 
           , std::forward<P>(params)...); 
} 

要打破递归调用,用空variant_type_list介绍过载:

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

第一次给调用者打电话时,提供variant_types作为参数:

this->conditional_invoke0<F>(variant_type_list<variant_type::int32, variant_type::string>{} 
          , std::forward<P>(params)...); 

DEMO