C++ 函数嵌套模板

标签 c++ templates sfinae generic-programming qlist

我想编写一个可以接收任何类型的 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/

相关文章:

c++ - 类型特征 C++ 的错误/错误使用

c++ - "Overloading"带有 SFINAE 的构造函数

c# - 如何按参数化类型过滤而不考虑其参数类型?

c++ - 当方法可用时,我如何专门化模板?

c++ - 如何枚举进程中所有命名管道的名称?

c++ - 如何使用 gdb 找到全局变量的析构函数调用?

c++ - 读取 txt 文件并从第二列中找到第一列给定值的最小值

c++ - 抓取 tcp 数据包的序列号

html - 如何创建 DRY HTML?

c++ - 为什么可变参数模板在 C++ 中表现得像这样?