Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt and stl templates
Qt 6.11 is out! See what's new in the release blog

Qt and stl templates

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 274 Views
  • 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 Offline
    M Offline
    Milosz
    wrote on last edited by
    #1

    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
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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
      1
      • M Offline
        M Offline
        Milosz
        wrote on last edited by
        #3

        It is great,
        Thank you

        Best regards

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved