returns 所选类型的元组的类型函数
Type function that returns a tuple of chosen types
我实现了一个类型函数 Tuple
,它将 My_enum
值列表转换为相应类型的 std::tuple
:
#include <tuple>
enum My_enum{ t_int, t_double };
// Bind_type is a type function that given a My_enum returns the corresponding type
template<My_enum E> struct Bind_type;
template<> struct Bind_type<t_int>{ using type = int; };
template<> struct Bind_type<t_double>{ using type = double; };
// Tuple is a type function that given a template value parameter pack of My_enums returns a std::tuple of correspondig types
template<My_enum First, My_enum... Others>
struct Tuple {
using type = decltype(std::tuple_cat(
typename Tuple<First>::type{},
typename Tuple<Others...>::type{}
));
};
template<>
struct Tuple<t_int> {
using type = std::tuple<Bind_type<t_int>::type>;
};
template<>
struct Tuple<t_double> {
using type = std::tuple<Bind_type<t_double>::type>;
};
我希望能够一次性声明 Tuple
的递归基本情况,因为我不想手动管理 Tuple
专业化,只要我添加或删除My_enum
的值,因为它容易出错(而且很无聊)。我试过:
template<My_enum E>
struct Tuple {
using type = std::tuple<Bind_type<E>::type>;
};
但这不是可变参数版本的有效特化。
我的问题是:当 Tuple
只有一个模板值参数时,是否有一种聪明的方法来声明它的特化?
无需递归,只需将参数包直接扩展为 std::tuple
:
template<My_enum... Enums>
struct Tuple {
using type = std::tuple<typename Bind_type<Enums>::type...>;
};
为了更直接地回答你的问题,你可以声明一个可变主模板,然后写两个特化:当至少有两个参数时,当只有一个时:
//primary template, takes any number of My_enums
template <My_enum... Enums>
struct Tuple {
//this case will be chosen if we instantiate a Tuple with no args
using type = std::tuple<>;
}
//specialization for when there are at least two arguments
template<My_enum First, My_enum Second, My_enum... Others>
struct Tuple<First,Second,Others...> {
using type = decltype(std::tuple_cat(
typename Tuple<First>::type{},
typename Tuple<Second,Others...>::type{}
));
};
//base case, only one argument left
template<My_enum Last>
struct Tuple<Last> {
using type = std::tuple<typename Bind_type<Last>::type>;
};
我实现了一个类型函数 Tuple
,它将 My_enum
值列表转换为相应类型的 std::tuple
:
#include <tuple>
enum My_enum{ t_int, t_double };
// Bind_type is a type function that given a My_enum returns the corresponding type
template<My_enum E> struct Bind_type;
template<> struct Bind_type<t_int>{ using type = int; };
template<> struct Bind_type<t_double>{ using type = double; };
// Tuple is a type function that given a template value parameter pack of My_enums returns a std::tuple of correspondig types
template<My_enum First, My_enum... Others>
struct Tuple {
using type = decltype(std::tuple_cat(
typename Tuple<First>::type{},
typename Tuple<Others...>::type{}
));
};
template<>
struct Tuple<t_int> {
using type = std::tuple<Bind_type<t_int>::type>;
};
template<>
struct Tuple<t_double> {
using type = std::tuple<Bind_type<t_double>::type>;
};
我希望能够一次性声明 Tuple
的递归基本情况,因为我不想手动管理 Tuple
专业化,只要我添加或删除My_enum
的值,因为它容易出错(而且很无聊)。我试过:
template<My_enum E>
struct Tuple {
using type = std::tuple<Bind_type<E>::type>;
};
但这不是可变参数版本的有效特化。
我的问题是:当 Tuple
只有一个模板值参数时,是否有一种聪明的方法来声明它的特化?
无需递归,只需将参数包直接扩展为 std::tuple
:
template<My_enum... Enums>
struct Tuple {
using type = std::tuple<typename Bind_type<Enums>::type...>;
};
为了更直接地回答你的问题,你可以声明一个可变主模板,然后写两个特化:当至少有两个参数时,当只有一个时:
//primary template, takes any number of My_enums
template <My_enum... Enums>
struct Tuple {
//this case will be chosen if we instantiate a Tuple with no args
using type = std::tuple<>;
}
//specialization for when there are at least two arguments
template<My_enum First, My_enum Second, My_enum... Others>
struct Tuple<First,Second,Others...> {
using type = decltype(std::tuple_cat(
typename Tuple<First>::type{},
typename Tuple<Second,Others...>::type{}
));
};
//base case, only one argument left
template<My_enum Last>
struct Tuple<Last> {
using type = std::tuple<typename Bind_type<Last>::type>;
};