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

Thiago Macieira thiago.macieira at intel.com
Mon Jan 2 14:18:52 CET 2017


On segunda-feira, 2 de janeiro de 2017 13:02:12 BRST René J.V. Bertin wrote:
> Hi,
> 
> I must be overlooking or even forgetting something: is there a way to
> initialise a QMap or QHash instance with a static table, say when you're
> creating a fast lookup dictionary mapping enums or preprocessor tokens to a
> QString?
> 
> That's information known a compile time so I was expecting to be able to
> avoid adding each mapping explicitly with a sequence of
> 
> map[token] = "token";
> 
> statements. If that's indeed possible, the documentation doesn't really
> showcase it.

The closest thing you have is the std::initializer_list constructor:

static const QHash<Enum, QString> data = {
	{ Value1, "Value1" },
	{ Value2, "Value2" }
};

Note that all of this is constructed at *load* time, not at compile time and 
not on first use.

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




More information about the Interest mailing list