从类模板中提取模板模板参数和可变参数模板参数 [英] Extract template template parameter and variadic template parameter from class template
问题描述
给定以下类模板:
template <template <typename... Args> class Container, typename... Args>
struct container_type_holder {};
我想提取它的模板模板参数和它的可变参数以在另一个上下文中重用.示例:
I would like to extract its template template parameter and its variadic parameter to reuse in another context. Example:
using c1 = container_type_holder<std::map, std::string, int>;
using c2 = container_type_holder<tt_parameter<c1>, vt_parameter<c1>>;
其中 tt_parameter
是从 c1 和 vt_parameter
中提取模板模板参数以提取其可变参数模板参数的魔术技巧.
Where tt_parameter<c1>
is some magic trick to extract the template template parameter from c1 and vt_parameter<c1>
to extract its variadic template parameter.
为了提取模板模板参数,我尝试在 container_type_holder
中添加一个别名,但这不起作用,因为它不是一个完整的类型.为了提取可变参数模板参数,我尝试了相同的策略,但没有成功.
In order to extract the template template parameter I tried to add an alias inside container_type_holder
, but that didn't work because it is not a complete type. To extract the variadic template parameter I tried the same strategy but with no success.
template <template <typename... Args> class Container, typename... Args>
struct container_type_holder
{
using container = Container; // doesnt work
using args = Args...; // ???
};
我不知道这是否可行,我是模板世界的初学者.
I don't know if this is possible or not, I'm a begginer in the template world.
推荐答案
您可以使用这些别名来允许检索模板参数:
You might use those aliases to be allowed to retrieve template parameters:
template <template <typename... Args> class Container,
typename... Args>
struct container_type_holder
{
template <typename ... Ts>
using container = Container<Ts...>;
constexpr std::size arg_count = sizeof...(Args);
using args_as_tuple = std::tuple<Args...>;
template <std::size_t I>
using get_arg = typename std::tuple_element<I, std::tuple<Args...>::type;
// And possibly helpers, such as
template <template <typename ...> OtherContainer>
using template_rebind = container_type_holder<OtherContainer, Args...>;
};
然后,用法可能是:
using c1 = container_type_holder<std::map, std::string, int>;
using c2 = c1::template_rebind<std::unordered_map>;
using c3 = container_type_holder<std::vector, std::pair<c1::get_arg<0>, c1::get_arg<1>>>;
这篇关于从类模板中提取模板模板参数和可变参数模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!