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

René J. V. Bertin rjvbertin at gmail.com
Mon Jan 2 15:15:11 CET 2017


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*"?
Not really an issue if the QHash or QMap instance can be a class member variable 
and initialised in the ctor.

> It’s an interesting point though, if you have a fixed set of data then you 
could use a perfect hash and a perfect number of buckets.

You already have a perfect hash in the case of a set of unique keys, like an 
enum, no? I've never used gperf, wouldn't that be more appropriate for (really) 
large key/value sets?

R.




More information about the Interest mailing list