c ++ value_type不适用于std :: map中的std :: tr1:tuple [英] c++ value_type not work for std::tr1:tuple in a std::map

查看:195
本文介绍了c ++ value_type不适用于std :: map中的std :: tr1:tuple的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段在Visual Studio 2008中使用,但在Visual Studio 2010中不能使用。

The following code snippet work with Visual Studio 2008 but not with 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:'initializing':无法从'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> 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 :: _ _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>

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):编译类模板成员函数'std :: _ Pair_base< _Ty1,_Ty2> :: _ Pair_base(const
std :: tr1 :: tuple <_Arg0,_Arg1>&&,int& br>
[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> with 1>

[1> _Ty1 = const MyValueType,1> _Ty2 = int 1>
] 1>

D:\Projekte\demo\Demo.cpp b $ b:参见类模板实例化'std :: pair< _Ty1,_Ty2>'
正在被编译1> with 1> [1>

_Ty1 = const MyValueType,1> _Ty2 = int 1>] 1> D:\Projekte\demo\Demo.cpp(39)
:编译类模板成员函数'void
MyStruct :: set(TKey& 1>与1> [1>

TKey = MyValueType 1>] 1>

D:\Projekte\demo\Demo.cpp(92)
:参见类模板实例化'MyStruct'为
编译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不能是
初始化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>]
========== Build:0成功,1失败,0最新,0已跳过==========

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

如果替换typedef std :: tr1 :: tuple MyValueType with typedef int MyValueType it works。

If a replace typedef std::tr1::tuple MyValueType with typedef int MyValueType it works.

提前感谢。 / p>

Thank you in advance.

推荐答案

我认为这是一个错误,与MSVC 2010中移动语义的错误实现,因为你可以成功地编译这个代码: / p>

I think this is an error that related to bad implementation of move semantic in MSVC 2010 since you can successfully compile this code:

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

这篇关于c ++ value_type不适用于std :: map中的std :: tr1:tuple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆