Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Qt and stl templates

    General and Desktop
    2
    3
    94
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Milosz last edited by

      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

      1 Reply Last reply Reply Quote 0
      • C
        ChrisW67 last edited by

        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?

        1 Reply Last reply Reply Quote 1
        • M
          Milosz last edited by

          It is great,
          Thank you

          Best regards

          1 Reply Last reply Reply Quote 0
          • First post
            Last post