如何检查两个参数包是否相同,忽略其内部顺序?
到目前为止,我只有框架(使用std :: tuple),但没有功能.
#include <tuple> #include <type_traits> template <typename,typename> struct type_set_eq : std::false_type { }; template <typename ... Types1,typename ... Types2> struct type_set_eq<std::tuple<Types1...>,std::tuple<Types2...>> : std::true_type { // Should only be true_type if the sets of types are equal }; int main() { using t1 = std::tuple<int,double>; using t2 = std::tuple<double,int>; using t3 = std::tuple<int,double,char>; static_assert(type_set_eq<t1,t1>::value,"err"); static_assert(type_set_eq<t1,t2>::value,"err"); static_assert(!type_set_eq<t1,t3>::value,"err"); }
每种类型不允许在一组中发生多次.
解决方法
如果元组中的类型是唯一的,则可以利用继承来回答,如果所有来自第一个元组的类型都作为辅助结构体的基础.例如. (C11方法):
#include <tuple> #include <type_traits> template <class T> struct tag { }; template <class... Ts> struct type_set_eq_helper: tag<Ts>... { }; template <class,class,class = void> struct type_set_eq: std::false_type { }; template <bool...> struct bool_pack { }; template <bool... Bs> using my_and = std::is_same<bool_pack<Bs...,true>,bool_pack<true,Bs...>>; template <class... Ts1,class... Ts2> struct type_set_eq<std::tuple<Ts1...>,std::tuple<Ts2...>,typename std::enable_if< (sizeof...(Ts1) == sizeof...(Ts2)) && my_and< std::is_base_of<tag<Ts2>,type_set_eq_helper<Ts1...>>::value... >::value >::type >: std::true_type { }; int main() { using t1 = std::tuple<int,"err"); }