3

I have tested the following two forms with clang and they are both accepted:

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}
};

On Visual Studio 2013, the latter example fails to compile stating there is no constructor in map that takes 4 arguments.

I'm assuming both are actually valid. What is the difference between the two? Why doesn't the second example work on Visual Studio 2013?

void.pointer
  • 24,859
  • 31
  • 132
  • 243
  • 2
    Both are legal. The first style may occasionally cause [weird things to happen](http://stackoverflow.com/questions/24112281/c11-initializer-list-fails-but-only-on-lists-of-length-2/24112395#24112395). If the second doesn't work on VS2013 it's a compiler bug. – T.C. Dec 30 '14 at 15:40
  • 1
    @T.C. Great read; thanks for the link. Unfortunately I think I need to "risk" it here since I need this code working on MSVC *and* Clang (unless you can suggest a reasonable alternative). If you post an answer with this I would be more than happy to give you a green checkmark! – void.pointer Dec 30 '14 at 15:47
  • @T.C.Could you please post your comment as an answer to my question? If you do, I'll mark yours as the answer. Thanks. – void.pointer May 26 '19 at 01:09

1 Answers1

1

As T.C. pointed out in comments, both are legal. The first style can cause weirdness, but if the second fails for you in VS2013 then that's a compiler bug )or partial implementation).

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055