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.
smime.p7s
Description: S/MIME cryptographic signature
-- Development mailing list [email protected] https://lists.qt-project.org/listinfo/development
