简体   繁体   中英

Cannot create unordered_map of tuple<int, int, int>

I'm using std::unordered_map<std::tuple<int, int, int>, float> . However, VS2010 will not compile the instantiation. I explicitly provided a hash specialization and an operator== overload. The error message refers to

std::unordered_map<NodeType, float> g_score;

And here is the full text of the error:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(127): error C2440: 'initializing' : cannot convert from 'const NodeType' to 'int'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxtuple0(9) : see reference to function template instantiation 'std::tr1::_Cons_node<_Car,_Cdr>::_Cons_node<_Ty,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&>(_Farg0 &&,_Farg1,_Farg2,_Farg3,_Farg4,_Farg5,_Farg6,_Farg7,_Farg8,_Farg9)' being compiled
1>          with
1>          [
1>              _Car=int,
1>              _Cdr=std::tr1::_Cons_node<int,std::tr1::_Cons_node<int,std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type>>,
1>              _Ty=NodeType,
1>              _Farg0=NodeType,
1>              _Farg1=std::tr1::_Nil &,
1>              _Farg2=std::tr1::_Nil &,
1>              _Farg3=std::tr1::_Nil &,
1>              _Farg4=std::tr1::_Nil &,
1>              _Farg5=std::tr1::_Nil &,
1>              _Farg6=std::tr1::_Nil &,
1>              _Farg7=std::tr1::_Nil &,
1>              _Farg8=std::tr1::_Nil &,
1>              _Farg9=std::tr1::_Nil &
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(145) : see reference to function template instantiation 'std::tr1::tuple<_Arg0,_Arg1,_Arg2>::tuple<const std::tr1::tuple<_Arg0,_Arg1,_Arg2>>(_Farg0 &&)' being compiled
1>          with
1>          [
1>              _Arg0=int,
1>              _Arg1=int,
1>              _Arg2=int,
1>              _Farg0=const std::tr1::tuple<int,int,int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(142) : while compiling class template member function 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base(const std::tr1::tuple<_Arg0,_Arg1,_Arg2> &&,float &&)'
1>          with
1>          [
1>              _Ty1=const NodeType,
1>              _Ty2=float,
1>              _Arg0=int,
1>              _Arg1=int,
1>              _Arg2=int
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(174) : see reference to class template instantiation 'std::_Pair_base<_Ty1,_Ty2>' being compiled
1>          with
1>          [
1>              _Ty1=const NodeType,
1>              _Ty2=float
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\unordered_map(279) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1>          with
1>          [
1>              _Ty1=const NodeType,
1>              _Ty2=float
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\unordered_map(275) : while compiling class template member function 'float &std::tr1::unordered_map<_Kty,_Ty>::operator [](const std::tr1::tuple<_Arg0,_Arg1,_Arg2> &)'
1>          with
1>          [
1>              _Kty=NodeType,
1>              _Ty=float,
1>              _Arg0=int,
1>              _Arg1=int,
1>              _Arg2=int
1>          ]
1>          c:\repo\render\render\sim\simcontext.cpp(155) : see reference to class template instantiation 'std::tr1::unordered_map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=NodeType,
1>              _Ty=float
1>          ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(127): error C2439: 'std::tr1::_Cons_node<_Car,_Cdr>::_Value' : member could not be initialized
1>          with
1>          [
1>              _Car=int,
1>              _Cdr=std::tr1::_Cons_node<int,std::tr1::_Cons_node<int,std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(170) : see declaration of 'std::tr1::_Cons_node<_Car,_Cdr>::_Value'
1>          with
1>          [
1>              _Car=int,
1>              _Cdr=std::tr1::_Cons_node<int,std::tr1::_Cons_node<int,std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type>>
1>          ]

Uncertain where to proceed, as I've never seen this error before. Any suggestions?

Edit: Interestingly, the unordered_set<NodeType> right before this compiles cleanly.

Try this for an SSCCE:

#include <deque>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

void GetPath() {
    typedef std::tuple<int, int, int> NodeType;
    struct node_hasher : public std::unary_function<const NodeType&, std::size_t> {
        std::size_t operator()(const NodeType& node) const {
            return std::hash<int>()(std::get<0>(node) + std::get<0>(node) + std::get<0>(node) + std::get<1>(node) + std::get<1>(node) + std::get<2>(node));
        }
    };
    std::unordered_set<NodeType, node_hasher> closed_set;
    std::unordered_map<NodeType, float, node_hasher> g_score;
    std::unordered_map<NodeType, float, node_hasher> f_score;
    std::unordered_map<NodeType, NodeType, node_hasher> came_from;
    auto node_comparator = [&](NodeType lhs, NodeType rhs) {
        return f_score[lhs] < f_score[rhs];
    };
}

int main() {
}

Fails to compile with the same error in the empty project. If you uncomment the node_comparator lambda, it works just fine. No wonder I had trouble with it- it's another VS bug.

This is a bug in the Visual C++ 2010 Standard Library. See the following report on Microsoft Connect:

C++ map<tuple<...>, V> compilation problems

The bug has been fixed for Visual Studio 11. Your example compiles without error using the Visual Studio 11 Beta.

Hint... implement hashing and equality as lambdas, then use the type of these lambdas as template parameters:

auto h = [](const TKey& x) { ... };
auto eq = [](const TKey& x, const TKey& y) { ... };

std::unordered_map<TKey, TValue, decltype(h), decltype(eq)> m;

Try moving NodeType and node_hasher to namespace scope. In C++03 local types can't be used as template arguments. Maybe VS2010 simply hasn't implemented that change yet. I'm simply guessing. I don't have VS2010 and thus haven't tried this experiment with your SSCCE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM