Philipp Neufeld
Philipp Neufeld

Reputation: 1103

Template template variadic parameter specialization

I was wondering if there was a way to specialize a template in the following way:

template<typename... Ts>
class Typelist { };

template<template<typename> typename... TTs>
class Typelist<TTs<typename>> { };   //I don't know if that is the right syntax, at least the compiler doesn't complain

I want it to work as a list of templated types as well as a list of non-templated types:

template<typename T>
class AT {};

template<typename T>
class BT {};

template<typename T>
class CT {};

int main() {

    using tl1 = Typelist<int, float, double>;   //works fine
    using tl2 = Typelist<AT, BT, CT>;   //gives an error

}

Edit:

If I declare the secont Typelist as a seperate type it works...

template<template<typename> typename... TTs>
class Typelist2 { };

//...
using tl2 = Typelist2<AT, BT, CT>;   //compiler doesn't complain and it works fine

I was wondering though if it was possible to use only Typelist for both cases so that Typelist2 doesn't have to be a seperate type.

Can anybody help me?

Upvotes: 1

Views: 97

Answers (1)

max66
max66

Reputation: 66200

I was wondering though if it was possible to use only Typelist for both cases so that Typelist2 doesn't have to be a seperate type.

I don't think so.

Because TypeList is defined as template<typename... Ts> or template<template<typename> typename... TTs>; both definition can't work togheter.

The best I can imagine to help you is define a TypeList base version for simple types

template <typename ... Ts>
struct TypeList
 {
   // here you can use Ts...
 };

and a specialization for containers (with only a type in it) as follows

template <template <typename> class ... Tts, typename ... Ts>
struct TypeList<Tts<Ts>...>
 {
   // here you can use Ts... and Tts...
 };

where you can use Ts and Tts.

But isn't a great solution because you can't define a containers TypeList simply as

TypeList<AT, BT, CT> tcl;

but you have to add contained dummy (?) types as follows

TypeList<AT<int>, BT<float>, CT<double>> tl2;

Another problem is you can't mix they, so

TypeList<AT<int>, BT<float>, double> tl;

call the base version (no containers) of TypeList.

Upvotes: 1

Related Questions