Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making constants in MISRA compliant C++
#8
Could I also suggest that when making constants, an exception might also be needed in Rule 0-1-6 (A project shall not contain instances of non-volatile variables being given values that are never subsequently used), eg.:

Code:
std::int32_t doIt_const3_neq0(const std::int32_t aVal)
{
  // Minimize scope to avoid violating Rule 3-4-1
  static const std::int32_t kConst3 = 7;

  return (aVal != 0) ? aVal : (aVal + kConst3);
}

In the code above, it is possible that kConst3 may be given a value, but that value is never subsequently used (it depends on the branches taken).

The program will not compile if kConst3 is not defined, so it is used by the compiler. If the correct set of branches are taken, it also effects program operation, so it is not dead code.

But, its usage can only be determined based on each individual program execution - some days it may not be used, other days it may be used.

However, the alternative (to avoid breaching rule 0-1-6) seems to be to use literal (magic) values directly in the code, which does not match current programming practice.
<t></t>
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)