如何使用多维初始化列表初始化自定义容器?

How to initialize a custom container with a multi-dimensional initializer list?

我目前正在使用 ContainsValue() 等其他方法为 std::map 类型开发自定义包装器。但是因为我试图让它尽可能与 std::map 兼容,所以我想知道是否可以用 "multi-dimensional initializer list".

初始化它

我的自定义地图类型定义如下:

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 */
  }
}

我说的是这样的初始化列表:

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

请注意,初始化中没有任何类型的转换。这一切都是自动发生的;与原始 std::map 类型一样。

当然,让构造函数采用 std::initializer_list<value_type> 就像 std::map 有:https://en.cppreference.com/w/cpp/container/map/map

或转发所有构造函数:Forwarding all constructors in C++0x