如何在可变模板的情况下应用模板模板参数
How to apply template template parameters in case of variadic template
我正在处理模板 MyTemplate
,它将按以下方式使用:
using Alias = MyTemplate<std::pair<int, char>,
std::pair<int, double>,
std::pair<int, unsigned int>>;
现在我们可以假设 MyTemplate
将只接受一对类型的列表。此外,所有对类型都应具有相同的第一种类型,第二种类型必须不同。
我想 "catch" MyTemplate
中的 first_type
对。我想出了以下定义:
template<typename... Ts>
struct MyTemplate
{
using first_type = std::decay_t<decltype((Ts::first_type, ...))>;
// ...
};
当我在不同模板的上下文中使用 typename MyTemplate::first_type
时,一切正常。我只收到警告:
warning: left operand of comma operator has no effect [-Wunused-value]
using first_type = std::decay_t<decltype((Ts::first_type, ...))>;
^
但是,我无法创建 Alias
的实例,因为我收到以下错误:
error: missing 'typename' prior to dependent type name 'std::pair<int, char>::first_type'
您对 "generic" 解决方案有什么想法吗?我可以使用 C+17.
For now we can assume that MyTemplate
will take only list of pair types. Moreover, all pair types should have the same first type and second type have to vary.
I want to "catch" the pairs' first_type
in MyTemplate
.
不确定,但在我看来你看起来像下面这样
template <typename...>
struct MyTemplate;
template <typename FT, typename ... STs>
struct MyTemplate<std::pair<FT, STs>...>
{
using first_type = FT;
};
你可以验证
using Alias = MyTemplate<std::pair<int, char>,
std::pair<int, double>,
std::pair<int, unsigned int>>;
static_assert( std::is_same<int, typename Alias::first_type>::value, "!" );
这样你也强加了 MyTemplate
的模板参数都是 std::pairs
s 和共同的 first_type
.
否则,如果没有模板专业化和维护方式,您可以使用 std::tuple
/std::tuple_element_t
template <typename ... Ts>
struct MyTemplate
{
using first_type = std::tuple_element_t<0, std::tuple<typename Ts::first_type...>>;
};
我正在处理模板 MyTemplate
,它将按以下方式使用:
using Alias = MyTemplate<std::pair<int, char>,
std::pair<int, double>,
std::pair<int, unsigned int>>;
现在我们可以假设 MyTemplate
将只接受一对类型的列表。此外,所有对类型都应具有相同的第一种类型,第二种类型必须不同。
我想 "catch" MyTemplate
中的 first_type
对。我想出了以下定义:
template<typename... Ts>
struct MyTemplate
{
using first_type = std::decay_t<decltype((Ts::first_type, ...))>;
// ...
};
当我在不同模板的上下文中使用 typename MyTemplate::first_type
时,一切正常。我只收到警告:
warning: left operand of comma operator has no effect [-Wunused-value]
using first_type = std::decay_t<decltype((Ts::first_type, ...))>;
^
但是,我无法创建 Alias
的实例,因为我收到以下错误:
error: missing 'typename' prior to dependent type name 'std::pair<int, char>::first_type'
您对 "generic" 解决方案有什么想法吗?我可以使用 C+17.
For now we can assume that
MyTemplate
will take only list of pair types. Moreover, all pair types should have the same first type and second type have to vary.I want to "catch" the pairs'
first_type
inMyTemplate
.
不确定,但在我看来你看起来像下面这样
template <typename...>
struct MyTemplate;
template <typename FT, typename ... STs>
struct MyTemplate<std::pair<FT, STs>...>
{
using first_type = FT;
};
你可以验证
using Alias = MyTemplate<std::pair<int, char>,
std::pair<int, double>,
std::pair<int, unsigned int>>;
static_assert( std::is_same<int, typename Alias::first_type>::value, "!" );
这样你也强加了 MyTemplate
的模板参数都是 std::pairs
s 和共同的 first_type
.
否则,如果没有模板专业化和维护方式,您可以使用 std::tuple
/std::tuple_element_t
template <typename ... Ts>
struct MyTemplate
{
using first_type = std::tuple_element_t<0, std::tuple<typename Ts::first_type...>>;
};