传递模板模板参数

Red*_*one 1 c++ templates variadic-templates c++17

假设我们有一个名为的类TypeCollection,它包含类型的打包模板:

template<typename ...Types>
class TypeCollection {};
Run Code Online (Sandbox Code Playgroud)

如果我们有一个模板 a 的类,TypeCollection您将需要做一些类似这样的事情:

template<template<typename ...> class Collection, typename ...Types>
class CollectionHandler {};
Run Code Online (Sandbox Code Playgroud)

它将像这样实例化:

CollectionHandler<TypeCollecion, A, B, C>
Run Code Online (Sandbox Code Playgroud)

这不太好,因为我们必须传递类型 AB 和 C 两次才能进行模板推导。我的问题是是否有一种方法可以做到这一点而不必两次传递类型:

CollectionHandler<TypeCollecion<A, B, C>>
Run Code Online (Sandbox Code Playgroud)

但是我似乎无法让它发挥作用。我尝试了一些事情,我意识到你不能将模板化类作为参数传递:

CollectionHandler<TypeCollecion<A, B, C>>  // Error: Template argument for template template parameter must be a class template or type alias template
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以实例化CollectionHandler而不必两次传递类型?我尝试使用元组来隐藏参数,但我也无法让它发挥作用。

感谢您的帮助!

Que*_*tin 5

翻转你的CollectionHandler声明来剖析TypeCollectionthrough 模板专业化:

template <class TypeCollection>
class CollectionHandler;

template <class... Types>
class CollectionHandler<TypeCollection<Types...>> { };
Run Code Online (Sandbox Code Playgroud)