c++qtconstexprcompile-time

Is possible create QString at compile time?


Consider below code:

static constexpr QString FOO = QStringLiteral("foo"); // erro compile , because QString has not default destructor.

How I can create QString at compile time!? is it possible to create QString at compile time?


Solution

  • In Qt 6.2, you can use the u"my string"_qs syntax.

    However, that has been deprecated in Qt 6.8, see Obsolete Members for QtLiterals — QtLiterals::operator""_qs(const char16_t *str, size_t size)


    Since Qt 6.4, you can use the new u"my string"_s syntax from the Qt::Literals::StringLiterals namespace.

    using namespace Qt::Literals::StringLiterals;
    auto str = u"hello"_s;
    

    See Qt Documentation — StringLiterals Namespace Qt::Literals::StringLiterals — StringLiterals::operator""_s(const char16_t *str, size_t size).