Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 586
Zimano Avatar asked Dec 11 '22 11:12

Zimano


2 Answers

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.

like image 194
songyuanyao Avatar answered May 16 '23 06:05

songyuanyao


You can't use it in the same way because they are not the same thing. A std::string is actually an alias for std::basic_string<char>, that is why you do not have to specify a template type when using it.

Since Qlist is not an alias and is a template type you have to use

QList<some_type>::size_type

One thing you could do is make your own alias like

using QIntList = QList<int>;

And then you can ise

QIntList::size_type
like image 25
NathanOliver Avatar answered May 16 '23 05:05

NathanOliver