[Development] Recommended way to take in strings

Marc Mutz marc.mutz at qt.io
Wed May 31 09:17:21 CEST 2023


On 31.05.23 09:07, Sami Varanka via Development wrote:
> What is the recommended way for functions to take strings? Our QtGraphs 
> API takes in const QString & but is recommended way nowadays take in 
> QAnyStringView?

I doubt there's an accepted project-wide standard, yet, but as a rule of 
thumb that everyone might be able to agree on: If the function doesn't 
store the string as-is (=parses or pre-processes it), take by 
QAnyStringView, otherwise continue to take by QString cref.

For very important APIs, there's the option to overload on

- `const QString &` and/or `QString &&`
- QAnyStringView

In this case, the QString overloads have to be marked Q_WEAK_OVERLOAD to 
avoid ambiguous calls with certain argument types (`const char*`, 
`QByteArray`, etc).

HTH,
Marc

-- 
Marc Mutz <marc.mutz at qt.io>
Principal Software Engineer

The Qt Company
Erich-Thilo-Str. 10 12489
Berlin, Germany
www.qt.io

Geschäftsführer: Mika Pälsi, Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht Charlottenburg,
HRB 144331 B



More information about the Development mailing list