gpt4 book ai didi

C++ 函数嵌套模板

转载 作者:行者123 更新时间:2023-11-28 02:49:34 31 4
gpt4 key购买 nike

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

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com