[Interest] initialising a dictionary-type container instance with a static table?

Thiago Macieira thiago.macieira at intel.com
Mon Jan 2 16:09:50 CET 2017


On segunda-feira, 2 de janeiro de 2017 15:15:11 BRST René J. V. Bertin wrote:
> Thiago Macieira wrote:
> 
> Oops, I didn't see all replies before answering to Konstantin..
> 
> > The closest thing you have is the std::initializer_list constructor:
> > 
> > static const QHash<Enum, QString> data = {
> > { Value1, "Value1" },
> > { Value2, "Value2" }
> > };
> 
> That's exactly what I was thinking about, and expecting to see among the
> examples in the QHash or QMap documentation.
> 
> > Note that all of this is constructed at *load* time, not at compile time
> > and not on first use.
> 
> so it doesn't work like a classical static variable which is initialised
> only once, presuming you meant to say "not on first use *only*"?

It is initialised once, at load time of the program. That means there's code 
that gets run before main(), whether you will use this or not.

That's different from initialising on first use, in which case you know that 
you will use it, though the first use has a slight performance penalty.

And that's different from compile-time initialisation (fully static data), in 
which there's no dynamic initialisation at all. QHash and QMap don't support 
that. That's what generators like gperf are for.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list