- 模板的模板参数:也就是说传入的模板参数也是一个模板
一 前提
- 模板作为参数时那首先就要定义出这个模板,才能被作为参数传递
- 为了后面看着方便这里就用x不用平时用到的T
- typename 只是指定类型名
template <typename x>
class templateBase
{
public:
void debugT1()
{
qDebug()<<m_t1;
}
void debugT2()
{
qDebug()<<m_t2;
}
private:
x m_t1;
x m_t2;
};
二 模板作为参数的声明
首先我们里面的template<typename x>相当于一个模板, 后面的class与typename功能一样,表示指定类型名为T,而这里的类型是前面的template<typename x>
- C17以前:
template <template<typename x>class T>
- C17以后
template <template<typename x>typename T>
三 示例
- 在调用的时候,我们需要传入一个模板类型
template <typename x>
class templateBase
{
public:
void debugT1()
{
qDebug()<<m_t1;
}
void debugT2()
{
qDebug()<<m_t2;
}
private:
x m_t1;
x m_t2;
};
template <template<typename x>class T>
class templateA
{
public:
void print();
private:
T<int> a;
T<float> b;
};
template <template<typename x>class T>
void templateA<T>::print()
{
qDebug()<<"templateA";
a.debugT1();
b.debugT1();
}
// 调用
templateA<templateBase> testTemplate;
testTemplate.print();