[Development] A QStringList puzzle for Monday

Jøger Hansegård joger.hansegard at qt.io
Mon Mar 17 20:09:31 CET 2025


> Obviously I am doing something wrong when setting o4 but it would be nice if I could get a compile error... 

Hi Henry,

Yes, this recently confused me as well, and here is what I found:

The issue is that QStringList("g","goessouth") behaves the same way as std::vector<QString>(“g","goessouth"). In both cases, the string literals collapse to pointers, and we call the QStringList(InputIt first, InputIt last) overload. The result will likely be a buffer overrun and possibly a crash. If you use the curly braces to initialize with an initializer list, the code does what you expect.

I agree that it would be nice to have a compiler error but given that QStringList behaves the same way as std::vector, I am not sure it is a bug.

Best regards,
Jøger


More information about the Development mailing list