获取模板列表的第一个模板的元函数 [英] Meta-Function to get the first template of a list of templates
问题描述
使用以下元函数 front
获取类型列表的第一个类型,我尝试编写一个类似的元函数来提取模板列表的第一个模板.
Using the following metafunction front
to get the first type of a typelist I try to write a similar metafunction to extract the first template of a list of templates.
namescpace detail {
template<typename L> struct front_impl;
template<template<typename, typename...> typename L, typename F, typename... I>
struct front_impl<L<F, I...>> {
typedef F type;
};
}
template<typename L> struct front_impl;
template<template<typename, typename...> typename L, typename F, typename... I>
struct front_impl<L<F, I...>> {
typedef F type;
};
您可以按如下方式使用它:
One can use this as follows:
template<typename... T>
struct List {
inline static constexpr size_t size = sizeof...(T);
};
using l1 = List<A, B, C>;
using f1 = front<l1>;
现在我尝试对模板列表做同样的事情.因此我使用了一个模板列表 TList
:
Now I try to do the same with a list of templates. Therefore I use a list of templates TList
:
template<template<typename> typename... TT>
struct TList {
inline static constexpr size_t size = sizeof...(TT);
};
和元函数tfront
:
template<typename T> struct tfront;
template<template<typename> typename F, template<typename> typename... R>
struct tfront<TList<F, R...>> {
template<typename T> using type = F<T>;
};
然后我可以提取模板列表中的第一个 A
、B
、...(此处未显示):
Then I can extract the first of a list of templates A
, B
, ... (not shown here):
using tlist = TList<A, B, C>;
template<typename T>
using f = typename tfront<tlist>::template type<T>;
f<int> xx;
然后 xx
属于 A
类型.
Then xx
ist of type A<int>
.
问题是:我可以用与 front
相同的方式编写元函数 tfront
,这不是对模板列表 TList
但是 模板的每个可变参数模板?所以我想为 tfront
引入一个参数 L
作为带有可变模板列表的模板模板参数,这样编译器也必须推导出输入 L
就像 front
一样.
The Question is: can I write the metafunction tfront
in the same way as front
, that is not as a partial specialization for the list of templates TList
but for every variadic template of templates? So I would like to introduce a parameter L
for tfront
as a template-template-parameter with a variadic list of templates, so that the compiler must also deduce the type of the type L
as in the case of front
.
我想写一些类似的东西(但用作什么???):
I would like to write something like (but what to use as ???):
template<template<typename> typename F, ??? TL, template<typename> typename... R>
struct tfront<TL<F, R...>> {
template<typename T> using type = F<T>;
};
推荐答案
你需要额外一层 template
:
template<typename>
struct tfront;
template<template<typename> typename F,
template<template<typename> typename...> typename TL,
template<typename> typename... R>
struct tfront<TL<F, R...>>
{
};
这篇关于获取模板列表的第一个模板的元函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!