VS2010 woes: tuples as map keys

Another day, another compiler/library bug. If you’re unfortunate enough to still be using Visual Studio 2010, don’t use tuples as map keys.

#include 
#include 
#include 

using namespace std;

typedef tuple FooT;
typedef map MapT;

int main(int, char*[])
{
  MapT m;

  // put a value in the map
  {
    FooT f(nullptr, 0);
    m[f] = 1337;
  }

  // retrieve a value
  int s;
  {
    FooT f(nullptr, 0);
    auto i = m.find(f);
    if (i != m.end())
    {
      // VS2010 says:
      // error C2440: 'initializing' : 
      // cannot convert from 'const FooT' to 'int *'
      s = i->second;
    }
  }

  return 0;
}

1 comment

  1. Something odd… if i replace

    m[f] = 1337;
    

    with either

    m.insert(std::make_pair(FooT(nullptr, 0), 1337));
    

    or

    m.insert(std::make_pair(f, 1337));
    

    It works fine (compiles and behaves like it should at runtime), even though the error reported by VS when it doesnt work points at the s assignment line!

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.