0

I'm currently working on a custom wrapper for the std::map type with additional methods like ContainsValue(). But since I try to make it as compatible with std::map as possible, I'd like to know if it's possible to initialize it with a "multi-dimensional initializer list".

My custom map type is defined like this:

template <typename TKey, typename TValue>
class CustomMap {
private:
  std::map<TKey, TValue> mapContent;

public:
  // Some interaction methods here

  void operator=(/* initialization type here */) {
    /* initialization here */
  }
}

And I'm talking about an initializer list like this:

CustomMap<uint64_t, std::string> test = {
  { 0xFF, "MaxByte" },
  { 0xFFFF, "MaxWord" },
  { 0xFFFFFFFF, "MaxDWord" },
  { 0xFFFFFFFFFFFFFFFF, "MaxQWord" }
};

Notice that there's no casting of any type in the initialization. It's all happening automatically; just as with the original std::map type.

1 Answers1

1

Sure, make a constructor taking std::initializer_list<value_type> like std::map has: https://en.cppreference.com/w/cpp/container/map/map

Or forward all constructors: Forwarding all constructors in C++0x

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I'm sorry, I don't really get it. Doesn't std::initializer_list<> just contain one single type? Then how exactly would I do this? To be honest, I don't even know how this works with std::map. –  Apr 25 '20 at 10:04
  • 1
    @RobinLe you can use `using value_type = std::pair` in your container (just like it's done in `std::map`). Afterwards, your `std::initializer_list` will actually take objects of the same type which is a pair of a key and a value. – passing_through Apr 25 '20 at 10:42
  • @passing_through Ah, alright, Thank you very much! :) –  Apr 25 '20 at 10:52