我想编写一个可以接收任何类型的 QList 和 QVector 的函数:
QList<int> iList;
QVector<int> iVector;
QList<double> dList;
QVector<double> dVector;
所有这些类型都必须支持调用
my_func(iList); my_func(iVector); my_func(dList); my_func(dVector);
我的解决方案
template <template <typename Elem> typename Cont>
void my_func(const Cont& cont)
{
qDebug() << cont.isEmpty();
Elem el = cont.first();
qDebug() << el;
}
未编译:
error C2988: unrecognizable template declaration/definition
这种模板函数的正确形式是什么?
最佳答案
您的代码中有几个错误:
template <template <typename> class Cont, typename Elem>
// ^^^^^ ^^^^^^^^^^^^^
void my_func(const Cont<Elem>& cont)
// ^^^^^^
{
qDebug() << cont.isEmpty();
Elem el = cont.first();
qDebug() << el;
}
关于C++ 函数嵌套模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23362671/