Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";
};
like image 620
Silicomancer Avatar asked Aug 27 '26 19:08

Silicomancer


1 Answers

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";
like image 101
Silicomancer Avatar answered Aug 29 '26 09:08

Silicomancer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!