0

How do I specify a template class as a default value for a template typename? e.g. the following doesn't work.

template <typename A, typename B> class X {};

template <typename T=template <typename, typename> class X> class Y {};

int main()
{
    Y<> y;
    return 0;
} 
tmp.cc:4:22: error: expected type-specifier before ‘template’
 template <typename T=template <typename, typename> class X> class Y {};
                      ^~~~~~~~
tmp.cc:4:22: error: expected ‘>’ before ‘template’
tmp.cc: In function ‘int main()’:
tmp.cc:8:7: error: template argument 1 is invalid
     Y<> y;
       ^
max66
  • 65,235
  • 10
  • 71
  • 111
user3689963
  • 115
  • 1
  • 6
  • 2
    It would be just `typename T = X` **if** `X` was a type. But it's not one, it's a template. Even if you remove the default argument and do `Y`, it won't compile. – HolyBlackCat May 23 '21 at 17:46
  • 1
    `T` is supposed to be a typename, but `X` is a class template, not a class. Either change it so that `Y`'s argument is a template instead of a typename, or change the default value to be a class instead of a class template. Hard to say which without knowing what `X` and `Y` are actually supposed to be. – Nathan Pierson May 23 '21 at 17:47
  • 1
    If it had worked, what should `X`'s template parameters `A` and `B` be when you try to instantiate it like this: `Y<> y;`? – Ted Lyngmo May 23 '21 at 17:49
  • 1
    This could be an [XY problem](https://meta.stackexchange.com/q/66377/950011) ... literally! – MatG May 23 '21 at 18:34

1 Answers1

1

The problem of your code is that for Y you ask a type template parameter and you want to use a template template parameter.

template <typename A, typename B> class X {};

//           type                template template
//........VVVVVVVVVV VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
template <typename T=template <typename, typename> class X> class Y {};

int main()
{
    Y<> y;
    return 0;
} 

If you want to use X as default, T must be be a template template, so

template <typename, typename>
class X
 {};

template <template <typename, typename> class T = X>
class Y
 {};

int main ()
 {
   Y<> y;
 } 

Or also using variadics to improve flexibility

template <template <typename...> class T = X>
class Y
 {};
max66
  • 65,235
  • 10
  • 71
  • 111