5

以下代码片段适用于 Visual Studio 2008,但不适用于 Visual Studio 2010。

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_;
};

用法

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

我收到以下错误:

1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\tuple(127): error C2440: 'initializing' : cannot convert from 'const MyValueType' to 'int' 1> 没有用户定义的转换运算符可用可以执行此转换,否则无法调用运算符 1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xxtuple0(9) :请参阅对函数模板实例化 '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)' 正在编译 1> 和 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) : 参见函数模板实例化 'std::tr1::tuple<_Arg0,_Arg1>::tuple>(_Farg0 &&) ' 正在编译 1>
与 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) : 在编译类模板成员函数'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) : 请参阅类模板实例化 'std::_Pair_base<_Ty1,_Ty2 >' 正在编译 1> 与 1>
[ 1> _Ty1=const MyValueType, 1> _Ty2=int 1> ] 1>
D:\Projekte\demo\Demo.cpp(40) :请参阅对类模板实例化 'std::pair<_Ty1,_Ty2>' 的引用 1> 编译 1> [ 1>
_Ty1=const MyValueType, 1> _Ty2= int 1> ] 1> D:\Projekte\demo\Demo.cpp(39) : 编译类模板成员函数 'void MyStruct::set(TKey &)' 1> with 1> [ 1>
TKey=MyValueType 1> ] 1>
D:\Projekte\demo\Demo.cpp(92) : 请参阅正在编译的类模板实例化 'MyStruct' 1> 参考 1> [ 1>
TKey=MyValueType 1> ] 1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\tuple(127):错误 C2439:'std::tr1::_Cons_node<_Car,_Cdr>::_Value':成员无法初始化 1> 使用 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) : 见 'std::tr1::_Cons_node<_Car,_Cdr>::_Value' 1> 的声明 1>
[ 1> _Car=int, 1>
_Cdr=std::tr1::_Cons_node: :_Type> 1> ] ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

如果将 typedef std::tr1::tuple MyValueType 替换为 typedef int MyValueType 它可以工作。

先感谢您。

4

1 回答 1

2

我认为这是与 MSVC 2010 中移动语义的错误实现有关的错误,因为您可以成功编译此代码:

typename Keys::value_type v( key, 1 );
keys_.insert(v);
于 2012-10-22T16:39:25.483 回答