2

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

igel
  • 358
  • 1
  • 8
  • 1
    FWIW, this version https://godbolt.org/z/Q4Eeom compiles in gcc, clang, icc and msvc. It looks like there is a problem with alias template compatibility with variadic template template parameters. – n. m. could be an AI May 03 '20 at 15:00
  • Possible duplicate of https://stackoverflow.com/questions/30707011/pack-expansion-for-alias-template – n. m. could be an AI May 03 '20 at 15:10

0 Answers0