mercredi 27 mars 2019

How to instruct std::unordered_map's value construction for objects that don't have a default constructor

Suppose I have the following class:

struct Foo
{
    Foo(int bar) : bar(bar) {}

    int bar;
}

How can I instruct a std::unordered_map<int, Foo> map to construct Foo(100) whenever they are needed, eg:

  • map initialization
  • map operator []

In order for the following snippet to work:

std::unordered_map<int, Foo> map;

assertTrue(map[11223344].bar == 100);

Is this even possible?

Aucun commentaire:

Enregistrer un commentaire