I'm trying to write a version of std::conditional
that works for templates instead of types (if I understood correctly, std::conditional
will not work with templates out of the box ("type/value mismatch")), but my understanding of parameter packs is limited.
Anyways, here's what I did:
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...>; };
So far so good, let's test it.
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
}
So, what's going on with the second line not compiling? The error message is:
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...>; };
| ^~~~
I'm confused, it seems like gcc is unpacking class... Z
to Ta
, okay, but then it tries to unpack Ta
again (which must fail), but only if used in a std::conditional
oO
EDIT: here's a link to play with: https://wandbox.org/permlink/XJBjxUf2TLxaPyzq