简体   繁体   中英

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.

QList is a class template, that means you should specify the template argument when use it, eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM