Search code examples
c++qttemplatesclass-templatedependent-name

Why can't I use QList::size_type as I would std::string::size_type? (template parameter error)


While researching an unsigned vs. signed integer comparison warning when declaring an iterator in my for loop, I read this:

Whenever possible, use the exact type you will be comparing against (for example, use std::string::size_type when comparing with a std::string's length).

I have a QList<T> I wanted to iterate over, declaring the iterator using the above method:

 for(QList::size_type i = 0; i < uploads.size(); i++)
 {
     //Do something
 }

And it gave me a compiler error:

error: 'template<class T> class QList' used without template parameters
for(QList::size_type i = 0; i < uploads.size(); i++)

Why can't I use it the same way? Is this caused by me or by Qt framework and its types? What is a good substitute for QList::size_type in this case, QList::size() just returns a regular old int and I wanted to use that; but I read the post linked above and it made me unsure.


Solution

  • QList is a class template, that means you should specify the template argument when use it, e.g. QList<int> and QList<int>::size_type.

    BTW: std::string is an instantiation of std::basic_string, which is a typedef defined as std::basic_string<char>. So std::string::size_type is equivalent to std::basic_string<char>::size_type in fact.