2012-07-10 17 views
8

Con il codice seguente, ottengo un errore molto confuso in MSVC che sembra suggerire il tipo di chiave (una std :: tupla) viene convertita in uno std: :stringa.Utilizzo di una tupla std :: come chiave per std :: unordered_map

#include <iostream> 
#include <string> 
#include <tuple> 
#include <utility> 
#include <unordered_map> 

typedef std::tuple<std::string,int,char> key_t; 

struct key_hash : public std::unary_function<key_t, std::size_t> 
{ 
    std::size_t operator()(const key_t& k) const 
    { 
     return std::get<0>(k)[0]^std::get<1>(k)^std::get<2>(k); 
    } 
}; 

struct key_equal : public std::binary_function<key_t, key_t, bool> 
{ 
    bool operator()(const key_t& v0, const key_t& v1) const 
    { 
     return (
       std::get<0>(v0) == std::get<0>(v1) && 
       std::get<1>(v0) == std::get<1>(v1) && 
       std::get<2>(v0) == std::get<2>(v1) 
      ); 
    } 
}; 

struct data 
{ 
    std::string x; 
}; 

typedef std::unordered_map<key_t,data,key_hash,key_equal> 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 << "x: " << itr->second.x; 
    } 
    return 0; 
} 

Errore:

Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'const std::tr1::tuple<_Arg0,_Arg1,_Arg2>' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\tuple 127 1 

Compiler: MS Visual Studio 2010

Su Ideone, ottengo il seguente errore ancora più contorta:

http://ideone.com/yEv2j

posso' Sembra che capisca dove ho sbagliato.

+0

Si noti che non è necessario per confrontare le chiavi come 'std :: get <0> (V0) == std :: get <0> (v1) && .....' Invece si può semplicemente scrivere 'ritorno V0 = = v1'. – Nawaz

+0

@Nawaz: Grazie per il commento, l'ho provato ma sto ancora ricevendo l'errore. – Gerdiner

+0

Quale riga sta causando l'errore, il primo con make_tuple o il secondo? L'errore IdeOne mostra che key_t viene ridefinito. Prova a rinominare il tipo. – Ajay

risposta

2

Strano. Il tuo codice funziona bene in Visual Studio 2012 RC e l'output è "x: dati di test".

+0

Avrei dovuto chiarire, sto usando MS Visual Studio 2010. – Gerdiner

+0

All'errore ideone è che /usr/include/sys/types.h:123:17: errore: 'key_t' ha una dichiarazione precedente come 'typedef __key_t key_t ', quindi funzionerà con un altro typedef, ma non so del 2012 VC. – ForEveR

+0

hai ragione, cambio il nome del tipo e lo compilo con ideone, sembra che ci possa essere un bug in msvc: http://ideone.com/olN9W – Gerdiner

4

Il problema per Ideone è che key_t esiste già:

prog.cpp:7:42: error: conflicting declaration 'typedef class std::tuple<std::basic_string<char>, int, char> key_t' 
/usr/include/sys/types.h:123:17: error: 'key_t' has a previous declaration as 'typedef __key_t key_t' 

Rinominare il key_t a qualcos'altro, o metterla in alcuni spazi dei nomi.

Your code works after this change sia in g ++ che in clang ++. Credo che questo sia un bug in MSVC.

Problemi correlati