【问题标题】:STL lower_bound not spec compliantSTL lower_bound 不符合规范
【发布时间】:2011-07-20 21:39:32
【问题描述】:

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

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;
}

【问题讨论】:

  • 错误 C2664: 'double mike::operator ()(const double,const char *) const' : 无法将参数 1 从 'const char [1]' 转换为 'const double' c:\程序文件\microsoft visual studio 8\vc\include\xutility 314
  • 除非你真的需要使用旧版本的 Visual C++,否则你应该使用 Visual C++ 2010。自 Visual C++ 2005 以来已经修复了很多很多错误已发布。

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


【解决方案1】:

这是 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 中修复。

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多