std::map 的这两种列表初始化形式有什么区别

What is the difference between these two forms of list initialization for std::map?

本文关键字:什么 区别 初始化 列表 map 两种 std      更新时间:2023-10-16

我已经用clang测试了以下两种形式,它们都被接受:

using IntMap = std::map<int, int>;
IntMap map1 {{
  {1, 1},
  {2, 2},
  {3, 3},
  {4, 4}
}};
IntMap map2 {
  {1, 1},
  {2, 2},
  {3, 3},
  {4, 4}
};

在 Visual Studio 2013 上,后一个示例无法编译,说明 map 中没有接受 4 个参数的构造函数。

我假设两者都是有效的。两者有什么区别?为什么第二个示例在 Visual Studio 2013 上不起作用?

正如T.C.在评论中指出的那样,两者都是合法的。第一种样式可能会导致怪异,但如果第二种样式在VS2013中失败,那么这就是编译器错误)或部分实现)。