Search code examples
c++qtconstexprstring-literals

Qt vs constexpr string literal


Is there a way to define as static constexpr string literal member in Qt? I.e. something like the following:

class X
{
   static constexpr QString tag = "mytag";
};

Solution

  • I did what Jesper recommended in his comment, I used QLatin1String. But I used it through a small helper class to avoid the strlen() call in QLatin1String:

    struct ConstLatin1String : public QLatin1String
    {
        constexpr ConstLatin1String(const char* const s) : 
            QLatin1String(s, static_cast<int>(std::char_traits<char>::length(s))) {}
    };
    

    This allows to do:

    static constexpr ConstLatin1String mystring = "foo";