Search code examples
c++stringqtqt5qstring

When constructing an empty QString in Qt, which way is the most efficient?


When constructing an empty QString in Qt,

  • QString()
  • QString("")
  • QLatin1String()
  • QLatin1String("")
  • QLatin1StringView()
  • QLatin1StringView("")
  • QStringLiteral()
  • QStringLiteral("")

Which one is the most efficient?


Solution

  • QString has "null" state, meaning there is no allocation for string data. You get a QString like this with default constructor. More "modern" way than QString() is QString{}.

    Short syntax is {}, if compiler can deduce the type. For example return {}; is an idiomatic way to return default-constructed value from function, without needing to repeat the type.

    Initializing with any string will do varying amounts of additional allocation, initilaization, encoding conversion.