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 astd::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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With