[Development] Unnecessary ambiguity in Qt documentation about reentrancy and safety of const methods.
Thiago Macieira
thiago.macieira at intel.com
Sat Jul 18 19:41:53 CEST 2026
On Saturday, 18 July 2026 02:54:42 Pacific Daylight Time NIkolai Marchenko
wrote:
> Case in point QCollator::compare which seemingly does unprotected lazy
> initialization inside a const function. It may be a bug but even Giuseppe
> had stated that Qt doesn't guarantee such use case and exceptions are
> possible here and there despite it "generally" being safe.
Strictly speaking, Giuseppe is right. But given the principle of least
surprise, we should avoid lazy initialisation in value-type classes precisely
because people assume they can call const methods from multiple threads. Lazy
initialisation in object-type classes is usually fine.
If the class is using a smart pointer for the Private, the smart pointer will
propagate the constness of the public function and it will be easy to notice
where we violate by way of a const_cast. Older classes often use raw pointers,
which don't propagate const, and then we may perform non-const operations,
intentionally or not.
QCollator is not only a raw pointer, it's a mutable one at that. Looking at
the implementation and logs, we've had bugs with that - see
199c49517e184db09cbb5eff60e15d73096b330b from 5.11 for example.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Principal Engineer - Intel DCG - Platform & Sys. Eng.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5176 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20260718/5400c315/attachment.bin>
More information about the Development
mailing list