[Interest] Can QObject::tr() be used in initialization of static const QString data members? [Qt 5.5.1, Windows]

Alejandro Exojo suy at badopi.org
Thu Nov 12 15:59:06 CET 2015


El Tuesday 10 November 2015, Thiago Macieira escribió:
> > What's the problem with static QString?
> 
> There's no actual error or problem, but it's just poor coding practice.

That certainly has surprised me. If you just mean that it could be abused, and 
that it could
 
> It's a static non-trivial type. We avoid those in Qt, so I make the 
> recommendation to everyone, everywhere.

Maybe not exactly a QString, but:

src/corelib/tools/qlocale_win.cpp
1108:        static QByteArray langEnvVar = qgetenv("LANG");
src/plugins/platforms/eglfs/deviceintegration/eglfs_kms/qeglfskmscursor.cpp
185:    static QByteArray json = qgetenv("QT_QPA_EGLFS_CURSOR");
src/plugins/platforms/eglfs/deviceintegration/eglfs_kms/qeglfskmsintegration.cpp
220:    static QByteArray json = qgetenv("QT_QPA_EGLFS_KMS_CONFIG");
src/plugins/platforms/eglfs/qeglfscursor.cpp
163:    static QByteArray json = qgetenv("QT_QPA_EGLFS_CURSOR");

> Static non-trivials imply a load-time and exit-time overhead, even if you 
> never use that variable again. Better to initialise on first use and make
> sure  you really need to cache the result.

But that doesn't apply to the typical use of them, isn't it? I mean that if 
you go that route, you'll do it because it's used often, or you'll destroy 
them in the destructor of a long lived class because you put them as a member 
variable. For example:

class Singleton {
    public:
        static Singleton& instance() {
            static Singleton instance;
            return instance;
        }

Or:

void Foo::bar() {
    static QLoggingCategory log("foo.bar");
    ...
    qCDebug(log) << "Lorem ipsum";


Or caching the result of a call to QStandardPaths because it makes accesses to 
the disk and you only need that check once. I remember in one case where I did 
this with a static local variable, but I ended up moving it to a member 
variable instead. So more or less the same destruction point in time, I think.

-- 
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net



More information about the Interest mailing list