[Interest] QVariantMap: constEnd() not equal to end()?

Jan Kundrát jkt at kde.org
Tue Mar 24 11:28:16 CET 2015


On Friday, 20 March 2015 07:25:48 CET, Aleksandr Mezin wrote:
> Table::const_iterator i(table.constFind(name));
> if (i != table.end()) {
>     ...
> }

end() returns an interator, not a const_iterator. Obtaining an interator 
(not a const_iterator) from a Qt container calls detach(), which means that 
your QHash gets copied.

There are multiple ways to fix this:

- Be sure you call constEnd() rather than end().
- Make sure that your QHash is a const. That way, you call the 
'const_iterator end() const' override, which cannot detach because your 
class is a const one.
- Build your code with -DQT_STRICT_ITERATORS=1 to disable casts and 
comparing of const vs. non-const iterators.

> This code doesn't work as I expect with Qt 5.4.1 MSVC 2013.
> The "if" is never entered, even when 'name' isn't found in the table.
> But it starts to work after replacing "end()" with "constEnd()".
> So, I guess, end() isn't equal to constEnd()? Is it an intended behavior?

Even though I know about the const_iterator vs. iterator, the behavior you 
reported feels wrong to me.

Cheers,
Jan

-- 
Trojitá, a fast Qt IMAP e-mail client -- http://trojita.flaska.net/



More information about the Interest mailing list