首页 > 解决方案 > 复合模板

问题描述

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

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
                        ^

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

标签: c++templatestemplate-templates

解决方案


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

你想要模板模板参数。例如

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

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

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

推荐阅读