c++ - 可变参数模板的 std::conditional
问题描述
我正在尝试编写一个std::conditional
适用于模板而不是类型的版本(如果我理解正确,std::conditional
将不适用于开箱即用的模板(“类型/值不匹配”)),但我对参数包的理解是有限的。
无论如何,这就是我所做的:
template<bool, template<class...> class X, template<class...> class Y>
struct conditional_template
{ template<class... Z> using type = X<Z...>; };
template<template<class...> class X, template<class...> class Y>
struct conditional_template<false, X, Y>
{ template<class... Z> using type = Y<Z...>; };
到目前为止一切顺利,让我们测试一下。
template<class> struct As {};
template<class> struct Bs {};
template<class Ta> using A = std::conditional_t<true, As<Ta>, double>;
template<class Tb> using B = Bs<Tb>;
template<bool choose,
template<class> class C = conditional_template<choose, A, B>::template type>
struct bla{};
int main()
{
bla<false> i; // <-- compiles in gcc 9.3, but not in clang
bla<true> j; // <-- does not compile in either gcc or clang
}
那么,第二行没有编译是怎么回事?错误信息是:
template.cpp:17:58: error: pack expansion argument for non-pack parameter 'Ta' of alias template 'template<class Ta> using A = std::conditional_t<true, As<Ta>, double>'
17 | struct conditional_template { template<class... Z> using type = X<Z...>; };
| ^~~~
我很困惑,似乎 gcc 正在解压class... Z
到Ta
,好吧,但随后它会尝试Ta
再次解压(必须失败),但前提是在std::conditional
oO中使用
编辑:这是一个可以使用的链接:https ://wandbox.org/permlink/XJBjxUf2TLxaPyzq