STL lower_bound 不符合规范
当宏 _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
返回: [first, last] 范围内最远的迭代器 i,对于 [first, i) 范围内的任何迭代器 j,满足以下相应条件: *j <值或 comp(*j, 值) != false
Visual Studio 错误消息
消息:错误 C2664:“double mike::operator ()(const double,const char *) const”:无法将参数 1 从“const char [1]”转换为“const double”
文件:c:\program files\microsoft Visual Studio 8\vc\include\xutility
行号: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;
}
The following code doesn't compile in C++Builder 2009 or in Visual C++ 2005 when the macro _HAS_ITERATOR_DEBUGGING
equals 1
but if commented out it will. It appears the lower_bound
function isn't spec compliant. The library is swapping the arguments. Here's an excerpt form the spec. value
should always be the second argument. Am I wrong?
NOTE: The test code wasn't designed to run. It was designed to illustrate the library bug.
C++ Spec excerpt
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 error message
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
Test Code
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Visual C++ 2005 C++ 标准库实现中的一个已知问题(请参阅 " 二进制谓词
lower_bound
的参数假定在 Microsoft Connect 上以调试模式进行编译时两个参数的类型相同”)。该错误已在 Visual C++ 2008 中修复。
This is a known issue in the Visual C++ 2005 C++ Standard Library implementation (see "Binary predicate paramter to
lower_bound
assumes that both parameters are the same type when compiling in debug mode" on Microsoft Connect).The bug was fixed in Visual C++ 2008.