当宏 _HAS_ITERATOR_DEBUGGING
等于 1
时,以下代码无法在 C++Builder 2009 或 Visual C++ 2005 中编译,但如果注释掉则可以。 lower_bound
函数似乎不符合规范。库正在交换参数。这是规范的摘录。 value
应该始终是第二个参数。我错了吗?
注意:测试代码不是为运行而设计的。它旨在说明库错误。
C++ 规范摘录
template<class ForwardIterator, class T, class Compare>
ForwardIterator
lower_bound(ForwardIterator first,
ForwardIterator last,
const T& value,
Compare comp);
25.3.3.1.3
Returns: The furthermost iterator i in the range [first, last] such that for any iterator j in the range [first, i) the following corresponding conditions hold: *j < value or comp(*j, value) != false
Visual Studio 错误消息
Msg: error C2664: 'double mike::operator ()(const double,const char *) const' : cannot convert parameter 1 from 'const char [1]' to 'const double'
File: c:\program files\microsoft visual studio 8\vc\include\xutility
Line No: 314
测试代码
#define _HAS_ITERATOR_DEBUGGING 1 // needs to be in the stdafx.h file for Visual Studio
#include "stdafx.h"
#include <algorithm>
#include <functional>
struct mike : public std::binary_function<double, char*, double> {
double operator() (const double i, const char*) const {
return i;
}
};
int main()
{
double r[] = {0};
std::lower_bound(r, r, "", mike());
return 0;
}
最佳答案
这是 Visual C++ 2005 C++ 标准库实现中的一个已知问题(参见 "Binary predicate paramter to lower_bound
assumes that both parameters are the same type when compiling in debug mode" on Microsoft Connect)。
此错误已在 Visual C++ 2008 中修复。
关于c++ - STL lower_bound 不符合规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6768885/