MISRA Discussion Forums
Rule 14-7-1 and templates that must be specialized - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.14 Templates (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=142)
+---- Thread: Rule 14-7-1 and templates that must be specialized (/showthread.php?tid=1619)



Rule 14-7-1 and templates that must be specialized - chgros - 29-04-2022

One of our clients is using code like this:

Code:
template<bool b> SomeClass;

template <> SomeClass<true> {
  void foo();
};
SomeClass<true> s;

We report a 14-7-1 defect, as non-specialized SomeClass is never instantiated.
They're complaining it's a false positive. Could we get a ruling? Arguably the explicit specialization and the non-specialization are different classes, and to be compliant both should be instantiated. On the other hand, this might be a reasonable thing to do if e.g. the goal is to prevent the code from working with <false> (note: I'm only guessing at the goal. The example from the customer doesn't go much further than this)
Do you have any guidance?


RE: Rule 14-7-1 and templates that must be specialized - misra cpp - 13-05-2022

The rule could be made clearer, but the intent was definitely to only cover templates that *are* instantiatable. Therefore, this could be seen as a false positive against a tool as there is no definition of SomeClass.

However, given modern C++ techniques, the presence of templates that are not intended to be instantiated (with or without definitions) makes this kind of rule Undecidable at best, with questionable value.

With this in mind, the rule is under consideration of the next version.