繁体   English   中英

c ++ value_type不适用于std :: map中的std :: tr1:tuple

[英]c++ value_type not work for std::tr1:tuple in a std::map

以下代码段适用于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):错误C2440:'初始化':无法从'const MyValueType'转换为'int'1>没有用户定义的转换运算符可用可以执行此转换,或者无法调用运算符1> C:\\ Program Files \\ Microsoft Visual Studio 10.0 \\ VC \\ include \\ xxtuple0(9):请参阅函数模板实例化'std :: tr1 :: _ Cons_node <_Car ,_Cdr> :: _ Cons_node <_Ty,性病:: TR1 :: _无&,性病:: TR1 :: _无&,性病:: TR1 :: _无&,性病:: TR1 :: _无&,性病:: TR1 :: _无&度,标准:: 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> with 1>
[1> _Car = int,1>
_Cdr = std :: tr1 :: _ Cons_node :: _ Type> 1>] ==========构建:0成功,1失败,0最新,0跳过======= ===

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

先感谢您。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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