2012-10-22 26 views
5

Các mã sau đoạn làm việc với Visual Studio 2008 nhưng không phải với Visual Studio 2010.C++ value_type không làm việc cho std :: tr1: tuple trong một std :: map

template <typename TKey> 
struct MyStruct 
{ 
    typedef std::map<TKey, int> Keys; 

    MyStruct() 
    { 
    } 

    void set(TKey& key) 
    { 
#if 1 
     // This works with VS 2008 but not with 2010 
     keys_.insert(typename Keys::value_type(key, 1)); 
#else 
     // This works with VS 2008 and VS 2010 
     keys_.insert(std::pair<TKey, int>(key, 1)); 
#endif 
    }; 

private: 
    Keys keys_; 
}; 

Cách sử dụng

typedef std::tr1::tuple<int, int> MyValueType; 
MyStruct<MyValueType> a; 
MyValueType x; 
a.set(x); 

tôi nhận được lỗi sau:

1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\tuple(127): error C2440: 'initializing' : cannot convert from 'const MyValueType' to 'int' 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1> C:\Program Files\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::_Type>, 1> _Ty=MyValueType, 1> _Farg0=MyValueType, 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\Microsoft Visual Studio 10.0\VC\include\utility(145) : see reference to function template instantiation 'std::tr1::tuple<_Arg0,_Arg1>::tuple>(_Farg0 &&)' being compiled 1>
with 1> [ 1> _Arg0=int, 1>
_Arg1=int, 1> _Farg0=const std::tr1::tuple 1> ] 1> C:\Program Files\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> &&,int &&)' 1> with 1>
[ 1> _Ty1=const MyValueType, 1> _Ty2=int, 1> _Arg0=int, 1> _Arg1=int 1> ] 1> C:\Program Files\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 MyValueType, 1> _Ty2=int 1> ] 1>
D:\Projekte\demo\Demo.cpp(40) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled 1> with 1> [ 1>
_Ty1=const MyValueType, 1> _Ty2=int 1> ] 1> D:\Projekte\demo\Demo.cpp(39) : while compiling class template member function 'void MyStruct::set(TKey &)' 1> with 1> [ 1>
TKey=MyValueType 1> ] 1>
D:\Projekte\demo\Demo.cpp(92) : see reference to class template instantiation 'MyStruct' being compiled 1> with 1> [ 1>
TKey=MyValueType 1> ] 1>C:\Program Files\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::_Type> 1> ] 1> C:\Program Files\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::_Type> 1> ] ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Nếu một thay thế typedef std :: tr1 :: tuple MyValueType với typedef int MyValueType nó hoạt động.

Cảm ơn bạn trước.

+2

Có vẻ là một lỗi biên dịch, nó [biên dịch trên gcc] (http : //liveworkspace.org/code/a21f9394d98fef376487a2e9ce1fc868). Bạn đang thiếu 'typename' trước' Keys :: value_type' (vì 'value_type' phụ thuộc vào' TKey'); nhưng thêm vào đó cũng không khắc phục được lỗi. – Praetorian

+1

Thông thường, tôi sử dụng 'keys_.insert (std :: make_pair (khóa, 1));' cho chèn như vậy. Điều này sẽ luôn luôn hoạt động. –

+0

Ồ, vâng tôi quên tên máy in. Tôi trích xuất MyStruct từ thư viện của bên thứ ba mà tôi đã sử dụng trong dự án của mình. Có tên. Vì vậy, tôi sửa mẫu ở trên. –

Trả lời

2

Tôi nghĩ rằng đây là một lỗi mà liên quan đến việc thực hiện xấu của động thái ngữ nghĩa trong MSVC 2010 kể từ khi bạn có thể biên dịch thành công mã này:

typename Keys::value_type v(key, 1); 
keys_.insert(v); 
+0

Cảm ơn bạn đã phản hồi nhanh. Có cách nào khác để biên dịch mã mà không thay đổi MyStruct. Vì tôi đã trích xuất MyStruct từ thư viện của bên thứ ba (Poco) mà tôi đã sử dụng trong dự án của mình. –

Các vấn đề liên quan