简体   繁体   English

为什么我不能使用operator <on'std :: deque'?

[英]Why can't I use operator< on 'std::deque'?

Ran cppcheck on my code base and received the following error: 在我的代码库上运行cppcheck并收到以下错误:

Dangerous iterator comparison using operator< on 'std::deque'.

But a deque's iterator is a random access iterator, and random access iterators support inequality operators. 但是deque的迭代器是一个随机访问迭代器,随机访问迭代器支持不等式运算符。 So what gives? 什么给出了什么?

Example: 例:

#include <deque>

int main()
{
    std::deque<int> d;
    std::deque<int>::iterator di1 = d.begin();
    std::deque<int>::iterator di2 = d.end();

    if (di1 < di2)
    {
        // (error) Dangerous iterator comparison using operator< on 'std::deque'.
    }

    return 0;
}

Edit: This bug was submitted and fixed via cppcheck ticket #5926 . 编辑:此错误已通过cppcheck ticket#5926提交并修复。

It's a bug in cppcheck. 这是cppcheck中的一个错误。

If we look at the code for the rule stlBoundaries() , the containers it triggers on are: 如果我们查看规则stlBoundaries()的代码,它触发的容器是:

"bitset|deque|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset"

However, in addition to deque , priority_queue is also guaranteed to have random access iterators. 但是,除了dequepriority_queue还保证具有随机访问迭代器。

The rationale for this rule is that programmers might accidentally write: 这条规则的基本原理是程序员可能会意外地写:

for (auto it = container.begin(); it < container.end(); ++it)
    ...

by analogy with the equivalent integer-indexed for loop, and this might actually compile for non-random-access iterators with some sort of conversion to pointer. 类似于等价的整数索引for循环,这可能实际上编译为非随机访问迭代器,并进行某种转换为指针。

This is the original trac item that added the rule: http://sourceforge.net/apps/trac/cppcheck/ticket/247 and this ticket exempted vector : http://sourceforge.net/apps/trac/cppcheck/ticket/313 这是添加规则的原始trac项目: http//sourceforge.net/apps/trac/cppcheck/ticket/247和此票证免除vectorhttp//sourceforge.net/apps/trac/cppcheck/ticket/ 313

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM