lower_bound()函数不能在调试模式下编译



我面临的麻烦要在调试模式下编译一些代码,该代码使用std :: lower_bound()函数与比较运算符结合使用。

我在以后附加的小型代码段中隔离了错误:

#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct SPippo
{
  SPippo(string rsName, int rnValue);
  static const bool compareId(const SPippo& lhs, const int& rId){return lhs.iValue < rId;}
  string sName;
  int iValue;
};

SPippo::SPippo(string rsName, int riValue):
  sName(rsName)
,iValue(riValue)
{ 
}
struct sOrd
{
  const bool operator() (const SPippo& lhs, const int& rId) const {return SPippo::compareId(lhs, rId);}
  const bool operator() (const int& rId, const SPippo& lhs) const {return SPippo::compareId(lhs, rId);}
};
int main()
{
  vector<SPippo> vecPippo;
  vecPippo.push_back(SPippo("Minnie", 1));
  vecPippo.push_back(SPippo("Paperino", 2));
  vecPippo.push_back(SPippo("Pippo", 3));
  vecPippo.push_back(SPippo("Topolino", 4));
  vector<SPippo>::const_iterator cItLowerBound = std::lower_bound(vecPippo.begin(), vecPippo.end(), 2, sOrd());
  return 0;
}

我正在使用VS2008进行编译:使用释放配置时,一切正常。相反,在调试模式下,我会收到以下错误:

1>c:program files (x86)microsoft visual studio 9.0vcincludexutility(346) : error C2664: 'const bool sOrd::operator ()(const int &,const SPippo &) const' : cannot convert parameter 1 from 'SPippo' to 'const int &'
1>        Reason: cannot convert from 'SPippo' to 'const int'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>        c:program files (x86)microsoft visual studio 9.0vcincludexutility(1699) : see reference to function template instantiation 'bool std::_Debug_lt_pred<_Pr,SPippo,SPippo>(_Pr,_Ty1 &,_Ty2 &,const wchar_t *,unsigned int)' being compiled
1>        with
1>        [
1>            _Pr=sOrd,
1>            _Ty1=SPippo,
1>            _Ty2=SPippo
1>        ]
1>        c:program files (x86)microsoft visual studio 9.0vcincludexutility(1709) : see reference to function template instantiation 'void std::_Debug_order_single2<_InIt,_Pr>(_FwdIt,_FwdIt,_Pr,bool,const wchar_t *,unsigned int,std::forward_iterator_tag)' being compiled
1>        with
1>        [
1>            _InIt=std::_Vector_iterator<SPippo,std::allocator<SPippo>>,
1>            _Pr=sOrd,
1>            _FwdIt=std::_Vector_iterator<SPippo,std::allocator<SPippo>>
1>        ]
1>        c:program files (x86)microsoft visual studio 9.0vcincludealgorithm(2290) : see reference to function template instantiation 'void std::_Debug_order_single<_FwdIt,_Pr>(_InIt,_InIt,_Pr,bool,const wchar_t *,unsigned int)' being compiled
1>        with
1>        [
1>            _FwdIt=std::_Vector_iterator<SPippo,std::allocator<SPippo>>,
1>            _Pr=sOrd,
1>            _InIt=std::_Vector_iterator<SPippo,std::allocator<SPippo>>
1>        ]
1>        c:program files (x86)microsoft visual studio 9.0vcincludealgorithm(2314) : see reference to function template instantiation '_FwdIt std::_Lower_bound<std::_Vector_iterator<_Ty,_Alloc>,int,__w64 int,_Pr>(_FwdIt,_FwdIt,const int &,_Pr,_Diff *)' being compiled
1>        with
1>        [
1>            _FwdIt=std::_Vector_iterator<SPippo,std::allocator<SPippo>>,
1>            _Ty=SPippo,
1>            _Alloc=std::allocator<SPippo>,
1>            _Pr=sOrd,
1>            _Diff=__w64 int
1>        ]
1>        c:userscgabdocumentsvisual studio 2008projectssweettestsweettestmain.cpp(42) : see reference to function template instantiation '_FwdIt std::lower_bound<std::_Vector_iterator<_Ty,_Alloc>,int,sOrd>(_FwdIt,_FwdIt,const int &,_Pr)' being compiled
1>        with
1>        [
1>            _FwdIt=std::_Vector_iterator<SPippo,std::allocator<SPippo>>,
1>            _Ty=SPippo,
1>            _Alloc=std::allocator<SPippo>,
1>            _Pr=sOrd
1>        ]
1>c:program files (x86)microsoft visual studio 9.0vcincludexutility(348) : error C2664: 'const bool sOrd::operator ()(const int &,const  SPippo &) const' : cannot convert parameter 1 from 'SPippo' to 'const int &'
1>        Reason: cannot convert from 'SPippo' to 'const int'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://c:UserscgabDocumentsVisual Studio 2008ProjectsSweetTestSweetTestDebugBuildLog.htm"
1>SweetTest - 2 error(s), 0 warning(s)

使用VS2015或更高版本时,该代码也可以在调试模式下编译罚款。鉴于我被迫将VS2008用于我的项目,您是否认为有办法解决此问题而不放弃使用lower_bound()?

预先感谢您的帮助!

我认为问题在VS2008在Xutility中使用的代码中。实际上,错误指向以下摘录:

template<class _Pr, class _Ty1, class _Ty2> inline
bool __CLRCALL_OR_CDECL _Debug_lt_pred(_Pr _Pred, const _Ty1& _Left,     const _Ty2& _Right,
const wchar_t *_Where, unsigned int _Line)
{   // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
if (!_Pred(_Left, _Right))
    return (false);
else if (_Pred(_Right, _Left))
    _DEBUG_ERROR2("invalid operator<", _Where, _Line);
return (true);

o,测试了一个单个 pred 函数,反转参数顺序。但是,如果谓词接受相同类型的参数,则可以使用!

否!/em>。这可能解释了为什么代码在VS2008以外的其他版本中编译。

如果其他人偶然发现了这一点,则可以将" hack"解决此方法,这是禁用相应汇编单元的迭代器调试。以下代码剪切,如果放置在#include指令之前,请执行此操作:

#if defined(_MSC_VER) && defined(_DEBUG) && _MSC_VER <= 1500
#define _HAS_ITERATOR_DEBUGGING 0
#endif

相关内容

  • 没有找到相关文章

最新更新