复合模板

Compound template

本文关键字:复合      更新时间:2023-10-16

我正在尝试制作"复合"模板类型。像这样的东西

template <typename A, typename T>
class configurator
{
public:
configurator(const A<T> & adapter) : m_adapter(adapter) {}
private:
A<T> m_adapter;
};

编译器抱怨

error: expected ')'
configurator(const A<T> & adapter
^

为什么这不起作用?有可能让它工作吗?

A被声明为类型模板参数;不能将其用作模板名称并为其指定模板参数。

您需要模板模板参数,例如

template <template <typename> typename A, typename T>
class configurator

顺便说一句,如果A应该使用多个模板参数,您可以使用模板参数包指定A

template <template <typename...> typename A, typename T>
class configurator