c++ - Using a std::tuple as key for std::unordered_map -


With the code below, I get a very deceptive error in MSVC, which is a key type (a std :: Tuple) is being converted to a std :: string.

  #include & lt; Iostream & gt; #include & lt; String & gt; # Include & lt; Tupal & gt; # Include & lt; Utility & gt; # Include & lt; Unordered_map> Typedef std :: tuple & lt; Std :: string, int, char & gt; Key_t; Struct key_hash: public std :: unary_function & lt; Key_t, std :: size_t & gt; {Std :: size_t operator () (const key_t & amp; k) const {return std :: get & lt; 0 & gt; (K) [0] ^ std :: get & lt; 1 & gt; (K) ^ std :: get & lt; 2 & gt; (T); }}; Struct key_equal: public std :: binary_function & lt; Key_t, key_t, bool & gt; {Bull Operator () (const key_T and V0, Const key_T, and V1) Const {Return (Study :: Mill & lt; 0> (v0) == std :: Mill & lt; 0 & gt; (V1) & amp; amp; & amp; Stud :: Mill & Lt; 1 & gt; (v0) == std :: get & lt; 1 & gt; (v1) & amp; Study :: Mill & Lt; 2 & gt; (v0) == std :: get <2> (v1)); }}; Structure data {std :: string x; }; Typedef std :: unordered_map & lt; Key_t, data, key_hash, key_equal & gt; Map_t; Int main () {map_t m; Data d; D.x = "test data"; M [std :: make_tuple ("abc", 1, 'x')] = d; Auto itr = m.find (std :: make_tuple (std :: string ("abc"), 1, 'x')); If (m.end ()! = Itr) {std :: cout & lt; & Lt; "X:" & lt; & Lt; Itr- & gt; Second.x; } Return 0; }   

Error:

  Error 1 error C2664: 'std :: basic_string & lt; _Elem, _Traits, _Ax & gt; :: Basic_string (const std :: basic_string & lt; _Elem, _Traits, _Ax & gt; & amp; amp;;): Parameter1 is called 'const std :: tr1 :: tuple & lt; _Arg0, _Arg1, _Arg2 & gt; Can not change from. For 'const std :: basic_string & lt; _Elem, _Traits, _Ax & gt; & Amp; Compile: MS Visual Studio 2010  

On the ideone, the 'C: \ Program Files (x86)' \ Microsoft Visual Studio 10.0 \ VC \ include \ tuple 127 1

I get the following even more complex error:

I do not understand where I've been wrong.

The problem for Ideone is that the key_t already exists:
  prog.cpp: 7: 42: id = "text" error: conflicting declaration 'typed square class std :: tuple & lt; Std :: basic_string & lt; Char & gt ;, int, char & gt; Key_t '/usr/include/sys/types.h:123:17: Error:' key_t 'as the previous declaration is' typedef __key_t key_t'   

your key_t put something else, or in some namespaces.

In both G ++ and Clag ++ I believe that this is a bug in MSVC.

Comments