2

I want to initialize a map with map-values in the following way:

std::map<int, std::map<int, int>> map =
{
    { 1, { { 1, 0 }, { 2, 1 } } },
    { 2, { { 1, 1 }, { 2, 0 } } },
};

While this compiles without any error or warning, it raises an exception stating "map/set iterators incompatible". If I remove the second pair, i.e. { 2, { { 1, 1 }, { 2, 0 } } }, no exception is raised and map contains one pair with key 1 and a map containing the pairs (1, 0) and (2, 1) as its value.

I'm sure there is a quite good reason for this (at first glance) somehow strange behavior.


Solution

Nope, there's no good reason. It turned out to be one of the beautiful bugs in Visual Studio 2013.

0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
  • 1
    remove ',' comma at the end. – 101010 Jun 03 '14 at 13:12
  • 3
    It [works for me](http://coliru.stacked-crooked.com/a/ef37b18f0a8f1c83)... perhaps it is a bug with your compiler. – Mankarse Jun 03 '14 at 13:12
  • 3
    @40two trailing commas are fine in braced-init-lists. – ecatmur Jun 03 '14 at 13:14
  • @ecatmur I know they are OK, they aren't good habit though. – 101010 Jun 03 '14 at 13:18
  • @Mankarse I'm using Visual Studio 2013. – 0xbadf00d Jun 03 '14 at 13:22
  • 2
    hehe, the OP is right it breaks in VS. It's a bug. VS2013 has several issues with nested initializers lists. I think is fixed in update 1. – 101010 Jun 03 '14 at 13:23
  • @40two But I LOVE trailing commas! – 0xbadf00d Jun 03 '14 at 13:27
  • @0xbadf00d LOL it's OK with me, I don't want to keep lovers apart. – 101010 Jun 03 '14 at 13:28
  • 4
    Duplicate of [this one](http://stackoverflow.com/questions/23965565/how-to-do-nested-initializer-lists-in-visual-c-2013) (but I can't mark it as such because that question has no answers). @40two There's no *bad habit* in using trailing commas. They're quite convenient because you don't have to modify existing lines when adding a new initial value. Also extremely handy if you're ever writing a tool to autogenerate initial values. – Praetorian Jun 03 '14 at 13:38
  • @40two: Beyond convenience, I'd say that they are a *good* practice, as they allow line-based diffs to be significantly cleaner. – Mankarse Jun 03 '14 at 14:32
  • 1
    If this is the answer, please answer your own question. With details if possible. – david.pfx Jun 03 '14 at 14:59

1 Answers1

0

did you tried with c++11 option?

#include <iostream>
#include <map>

int main()
{
  std::map<int, std::map<int, int>> map =
    {
      { 1, { { 1, 0 }, { 2, 1 } } },
      { 2, { { 1, 1 }, { 2, 0 } } },
    };

  auto t = map[1];
  std::cout << t[1] << std::endl;
  std::cout << t[2] << std::endl;

  auto t2 = map[2];
  std::cout << t2[1] << std::endl;
  std::cout << t2[2] << std::endl;

  std::cout << "--------------" << std::endl;
  std::cout << map[1][1] << std::endl;
  std::cout << map[1][2] << std::endl;
  std::cout << map[2][1] << std::endl;
  std::cout << map[2][2] << std::endl;
}
Esus
  • 173
  • 9