考虑下面尝试从迭代器范围构造映射的代码:
#include <map>
template< typename C >
typename C::const_iterator cbegin( C const & c )
{
return c.begin();
}
template< typename C >
typename C::const_iterator cend( C const & c )
{
return c.end();
}
int main()
{
typedef std::map<const int, int > Map;
Map m1;
Map m2( m1.begin(), m1.end() );
Map m3( m1.begin(), m1.end(), std::less<const int>() );
Map m4( cbegin( m1 ), cend( m1 ), std::less<const int>() );
}
// cl -nologo -EHsc vc6-map.cpp && vc6-map
// g++ -Wall -Wextra -Weffc++ -std=c++11 -o vc6-map vc6-map.cpp && vc6-map
g++ 4.8.1 成功编译了代码,而 Visual C++ 6, SP6 (VC6) 无法构造 m2、m3 和 m4 中的每一个。
VC6 给出以下错误:
prompt>cl -nologo -EHsc vc6-map.cpp && vc6-map
vc6-map.cpp
vc6-map.cpp(22) : error C2664: '__thiscall std::map<int const ,int,struct std::less<int const >,class std::allocator<int
> >::std::map<int const ,int,struct std::less<int const >,class std::allocator<int> >(const struct std::less<int const >
&,const class std::allocator<int> &)' : cannot convert parameter 1 from 'class std::_Tree<int const ,struct std::pair<i
nt const ,int>,struct std::map<int const ,int,struct std::less<int const >,class std::allocator<int> >::_Kfn,struct std:
:less<int const >,class std::allocator<int> >::iterator' to 'const struct std::less<int const > &'
Reason: cannot convert from 'class std::_Tree<int const ,struct std::pair<int const ,int>,struct std::map<int co
nst ,int,struct std::less<int const >,class std::allocator<int> >::_Kfn,struct std::less<int const >,class std::allocato
r<int> >::iterator' to 'const struct std::less<int const >'
No constructor could take the source type, or constructor overload resolution was ambiguous
vc6-map.cpp(23) : error C2664: '__thiscall std::map<int const ,int,struct std::less<int const >,class std::allocator<int
> >::std::map<int const ,int,struct std::less<int const >,class std::allocator<int> >(const struct std::pair<int const ,
int> *,const struct std::pair<int const ,int> *,const struct std::less<int const > &,const class std::allocator<int> &)'
: cannot convert parameter 1 from 'class std::_Tree<int const ,struct std::pair<int const ,int>,struct std::map<int con
st ,int,struct std::less<int const >,class std::allocator<int> >::_Kfn,struct std::less<int const >,class std::allocator
<int> >::iterator' to 'const struct std::pair<int const ,int> *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
vc6-map.cpp(24) : error C2664: '__thiscall std::map<int const ,int,struct std::less<int const >,class std::allocator<int
> >::std::map<int const ,int,struct std::less<int const >,class std::allocator<int> >(const struct std::pair<int const ,
int> *,const struct std::pair<int const ,int> *,const struct std::less<int const > &,const class std::allocator<int> &)'
: cannot convert parameter 1 from 'class std::_Tree<int const ,struct std::pair<int const ,int>,struct std::map<int con
st ,int,struct std::less<int const >,class std::allocator<int> >::_Kfn,struct std::less<int const >,class std::allocator
<int> >::const_iterator' to 'const struct std::pair<int const ,int> *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
对于 m2,VC6 似乎无法调用正确的构造函数。这通过指定std::less
比较器对象来解决 m3 和 m4 的构造。m3 和 m4 的构造误差之间的唯一区别是iterator
与const_iterator
.
如何欺骗 VC6 来执行所需的操作?
2013 年 10 月 14 日编辑:VC6 地图范围构造函数定义如下:
typedef const value_type *_It;
map(_It _F, _It _L, const _Pr& _Pred = _Pr(), const _A& _Al = _A()) {...}
这澄清了错误:VC6 版本需要一个std::pair<> const *
不同于std::map<>::const_iterator
.