0

We have the following code:

class Foo {
    std::map<const uint8_t, double> m_throttle;
    // ...
    void bar(const uint8_t &msg_id)
    {
        if (m_throttle.find(msg_id) == m_throttle.end())
        {
            // Whatever
        }
    }
};

This produces the following error from the QA-C code checker when applying the MISRA C++ 2008 rules, highlighting the msg_id as the problematic token:

05-00-03 複合式が暗黙的に異なる本質型へ変換されています。

Or in English:

Complex expressions are implicitly converted to different intrinsic types.

According to the PDF here, fuller text is:

A cvalue expression shall not be implicitly converted to a different underlying type

However, the implementation of std::map::find() is just:

iterator
find(const key_type& __x)
{ return _M_t.find(__x); }

Where key_type should be uint8_t. Is this a bug in the tool, or is there something I am missing?

Note the tool uses the currently-installed gcc, apparently, which is 5.4.0.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 2
    Does switching to `m_throttle.cend()` affect the warning at all? – StoryTeller - Unslander Monica May 29 '19 at 07:33
  • 3
    If I recall correctly, maps' key and value are implemented as a pair of const key and value. You cannot change the key of a given value (that implies reordering, so you can delete and reinsert with a different key), so that makes the `const` part of the `const uint8_t` useless. – Mirko May 29 '19 at 07:40
  • @StoryTeller Goog point! There's also an error 5-0-1 (order of evaluation) on the `==`, which might be resolved with `cend()`, but that's a different issue. (The checker is on our CI server, so I cannot interactively see what happens.) – Ken Y-N May 29 '19 at 07:40
  • 1
    TBH, since nothing is const, this seems like a false positive on the checker's part. It seems to think erroneously that `find` will return a `const_iterator`. Can't tell you how to proceed though. – StoryTeller - Unslander Monica May 29 '19 at 07:42
  • 1
    @Mirko That's my colleague trying lots of different things, so yes, the `const` is redundant, although there might be another MISRA rule that recommends it. I get similar errors in my code without the `const`. – Ken Y-N May 29 '19 at 07:42
  • 1
    besides, using a 256-element array will be faster if your map has a lot of items – phuclv May 29 '19 at 07:49
  • Implicit conversion to different underlying type implies some manner of expression other than `==`. Are you sure the problem is in the part you posted and not elsewhere? – Lundin May 29 '19 at 08:57

0 Answers0