Search code examples
c++templatessfinaegeneric-programmingqlist

C++ Function Nested Templates


I would like to write a function that can receive both QList and QVector of any type:

QList<int> iList;
QVector<int> iVector;
QList<double> dList;
QVector<double> dVector;

all these types must support invocation

my_func(iList); my_func(iVector); my_func(dList); my_func(dVector);

My solution

template <template <typename Elem> typename Cont>
void my_func(const Cont& cont)
{
    qDebug() << cont.isEmpty();
    Elem el = cont.first();
    qDebug() << el;
}

is not compiled:

error C2988: unrecognizable template declaration/definition

What is the proper form of such template function?


Solution

  • There are several errors in your code:

    template <template <typename> class Cont, typename Elem>
    //                            ^^^^^       ^^^^^^^^^^^^^
    void my_func(const Cont<Elem>& cont)
    //                     ^^^^^^
    {
        qDebug() << cont.isEmpty();
        Elem el = cont.first();
        qDebug() << el;
    }