无法创建tuple <int,int,int>的unordered_map

Pup*_*ppy 2 c++ visual-studio-2010 visual-c++ c++11

我正在使用std::unordered_map<std::tuple<int, int, int>, float>.但是,VS2010不会编译实例化.我明确提供了hash专门化和operator==重载.错误消息指的是

std::unordered_map<NodeType, float> g_score;
Run Code Online (Sandbox Code Playgroud)

这是错误的全文:

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>          ]
Run Code Online (Sandbox Code Playgroud)

不确定在哪里继续,因为我以前从未见过这个错误.有什么建议?

编辑:有趣的是,unordered_set<NodeType>在此之前正确编译干净.

试试这个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() {
}
Run Code Online (Sandbox Code Playgroud)

无法在空项目中使用相同的错误进行编译.如果取消注释node_comparatorlambda,它就可以正常工作.难怪我遇到了麻烦 - 这是另一个VS bug.

Jam*_*lis 5

这是Visual C++ 2010标准库中的错误.请参阅以下有关Microsoft Connect的报告:

C++ map<tuple<...>, V>编译问题

已修复Visual Studio 11的错误.您的示例使用Visual Studio 11 Beta编译时没有错误.