Qt and stl templates
Solved
General and Desktop
-
Hi everybody,
I'd like to make a template to test a certain number of containers (both stl and Qt)
The declaration for Qt is:template<typename Tp, template<typename> typename Container> class ContainerTest { public: void test() { ... } private: Container<Tp> container; };
and sample specializations :
ContainerTest<qint16, QList> l(55); ContainerTest<qreal, QVector> li(55); ContainerTest<uint16_t, QVector> la(55); ContainerTest<int32_t, QList> lb(55);
Unfortunatelly this template cannot be used for stl templates like vector or list. The proper one is:
template<typename Tp, template<typename, typename=allocator<Tp>> typename Container> class ContainerTest { ... };
and sample specializations :
ContainerTest<qint16, vector> l(55); ContainerTest<qreal, list> li(55);
Can anyone show a solution path with just one template for both Qt and stl templates?
Thanks in advance
-
Since the second argument to the std::vector or std::list template has a default you can use C++11 variadic templates:
#include <QCoreApplication> #include <QVector> #include <QList> #include <vector> #include <list> template<typename Tp, template<typename...> typename Container> class ContainerTest { public: void test() { } private: Container<Tp> container; }; int main(int argc, char **argv) { QCoreApplication app(argc, argv); ContainerTest<uint32_t, QVector> la; ContainerTest<uint32_t, QList> lb; ContainerTest<uint32_t, std::vector> lc; ContainerTest<uint32_t, std::list> ld; return 0; }
This compiles fine for me with GCC 9.4.0 and Qt 5.12.8. Does it scratch your itch?