2012-10-22 16 views
5

Poniższy kod urywek pracy z Visual Studio 2008, ale nie z Visual Studio 2010.C++ VALUE_TYPE nie działać na std :: tr1: krotka w 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_; 
}; 

Wykorzystanie

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

mi się następujący błąd:

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 ==========

Jeśli zastąpić typedef std :: tr1 :: MyValueType krotka z typedef int MyValueType to działa.

Z góry dziękuję.

+2

wydaje się być błąd kompilatora, to [kompiluje na gcc] (http : //liveworkspace.org/code/a21f9394d98fef376487a2e9ce1fc868). Brakuje 'typename' przed' Keys :: value_type' (ponieważ 'value_type' zależy od' TKey'); ale dodanie tego również nie naprawi błędu. – Praetorian

+1

Zwykle używam 'keys_.insert (std :: make_pair (key, 1));' dla takich insertów. To powinno zawsze działać. –

+0

O tak, zapomniałem nazwy pliku. Wyodrębniłem MyStruct z biblioteki innej firmy, której użyłem w moim projekcie. Istnieje nazwa pliku. Naprawiłem próbkę powyżej. –

Odpowiedz

2

myślę, że to jest błąd, że podobne do złego wykonania ruchu semantycznej w MSVC 2010, ponieważ można z powodzeniem skompilować ten kod:

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

Dziękuję za szybkie odpowiedzi. Czy istnieje inny sposób kompilacji kodu bez zmiany MyStruct. Ponieważ wyodrębniłem MyStruct z biblioteki innej firmy (Poco), której użyłem w moim projekcie. –

Powiązane problemy