c++ - STL lower_bound 不符合规范

标签 c++ visual-c++ stl visual-c++-2005

当宏 _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/

相关文章:

C++ 指针数组声明

c++ - 使用 C++ Visual studio Community 2013 进行 Arduino 程序

c++ - 使用大括号而不是 make_pair 会出现错误

c++ - std::chrono:将自定义持续时间添加到 time_point

python - 如何有效地将大字符串从 Python 传递到 C++ 扩展方法?

c++ - 使用 C++ Stream API 发生访问冲突异常

c++ - 为什么动态创建 STL 容器被认为是不好的做法?

c++ - 模拟一堆卡片的最佳容器

c++ - 将工具提示添加到 ComboBoxEx 失败

c++ - 什么是 R6010 错误?