Home > Blockchain >  Passing template template parameters
Passing template template parameters

Time:10-04

Let's say we have a class called TypeCollection that holds a packed template of types:

template<typename ...Types>
class TypeCollection {};

And if we have a class that templates a TypeCollection you would need to do something a little like this:

template<template<typename ...> class Collection, typename ...Types>
class CollectionHandler {};

Which would be instantiated like so:

CollectionHandler<TypeCollecion, A, B, C>

This isn't great since we have to pass the types A B and C twice for template deduction. My question is if there is a way to do this without having to pass the types twice:

CollectionHandler<TypeCollecion<A, B, C>>

However I cant seem to get this to work. I tried a couple things and I realized that you cant pass a templated class as a parameter:

CollectionHandler<TypeCollecion<A, B, C>>  // Error: Template argument for template template parameter must be a class template or type alias template

Is there a way to instantiate CollectionHandler without having to pass the types twice? I experimented with tuples to hide the parameters but I couldn't get that to work either.

Thanks for your help!

CodePudding user response:

Flip your CollectionHandler declaration around to dissect the TypeCollection through template specialization:

template <class TypeCollection>
class CollectionHandler;

template <class... Types>
class CollectionHandler<TypeCollection<Types...>> { };

CodePudding user response:

TypeCollecion<A, B, C> is not a template. It is a type. If you change

template<template<typename ...> class Collection, typename ...Types>
class CollectionHandler {};

to

template<typename Collection>
class CollectionHandler {};

Then you can use TypeCollecion<A, B, C> as parameter. And TypeCollection should probably offer member aliases to access A, B and C.

You do not need a template template parameter when you actually want to use a type as parameter (whether this type is an instantiation of a template does not matter that much).

  • Related