0

I am coding in C++14. The company requires all code to pass Parasoft / Misra C++ 2008 checks.

I receive a string which ends in a digit from 1 to N and I need to convert it to uint8_t and subtract 1 to use it as an array index.

// NumberString is guaranteed to contain a single digit as a std::string
const uint8_t Number = static_cast<uint8_t>(std::stoi(NumberString) - 1);

causes Parasoft to report

Expression of 'signed' type should not be cast to 'unsigned' type

I have tried many ways to rewrite it, but to no avail. How can I get rid of that Parasoft message?

I am at my wit's end and even considering stuffing an extra (unused) element zer0 at the front of that array. Surely there must be a way to avoid that?

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 1
    I suspect you'll also get an error about truncating an `int` to a `uint8_t`. What I found in cases like that was that casting a complex expression was forbidden, but `int i = std::stoi(...); uint32_t u = static_cast<>(i) + 1u; uint8_t Number = static_cast<>(u);` or something like that would pass, although all that splitting made the code much more unreadable! – Ken Y-N Mar 16 '22 at 08:38
  • 1
    I wil give it a try. I already tried a few permutations. If it needs to be ugly to placate Parasoft, I will wrap the code in a helper function with a big "here be dragons" comment which explains the ugliness – Mawg says reinstate Monica Mar 16 '22 at 09:51
  • Just a quick suggest Mawg: "at wit's end" is a fairly intense statement that suggests one is about to lose ones sanity (the meaning of "wits" here). When this statement is used it indicates that the writer is in a great state of suffering, and the obvious implication is that readers are malign people for leaving them in that situation. It is generally better if (technical) writing does not try to impose any such moral obligation to help. – halfer Mar 20 '22 at 11:40
  • (FWIW I routinely remove it from any post). – halfer Mar 20 '22 at 11:45

0 Answers0