On terça-feira, 14 de março de 2017 09:03:11 PDT Gunnar Roth wrote: > Hello Qt and C++ experts. > > I try to let the compiler generate a QLatin1String instance from a string > literal. Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s) > Q_DECL_NOTHROW : m_size(s ? int(strlen(s)) : 0), m_data(s) {} > > cannot really do this as it uses strlen, is this right? Only > a QLatin1String(nullptr) would be generated at compile time.
More or less correct. strlen is not, officially, constexpr. I actually asked the C++ proposals mailing list about that last week and the conclusion was "we won't touch the C API". However, GCC actually implements the strlen builtin as constexpr. So you could use __builtin_strlen. Anyway, thanks for pointing this out. I've just submitted a change to drop that constexpr: https://codereview.qt-project.org/188478 > I can then do something like if > (arguments.contains(HmiGfx::RHQt::UtQt::Latin1Literal("--fullscreen"))) or > define a variable like Q_CONSTEXPR Latin1Literal MY_PREFIX("Prefix"); The former does not need to be constexpr. The second, I have to ask why you need it for. > My question is now, is this the right thing to do and could something like > that be added to Qt itself? I'm not sure. We could have added the array one and decided not to, as it changes behaviour: static const char data[] = "foo\0bar\0baz\0"; QLatin1String foo(data); QLatin1String bar(data + 4); Today, foo == "foo". If we add the overload, it becomes "foo\0bar\0baz\0". We've had this discussion with QString conversion from QByteArray: do we use QByteArray's length or do we strlen it? There were quite a few cases to be found that depended on the implicit NUL being found when we tried to change that, and we couldn't apply the change without too much breakage. -- Thiago Macieira - thiago.macieira (AT) intel.com Software Architect - Intel Open Source Technology Center _______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest