MISRA Discussion Forums
Rule 6.7.2 variable templates - 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++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188)
+---- Forum: 4.6 Basic concepts (https://forum.misra.org.uk/forumdisplay.php?fid=192)
+---- Thread: Rule 6.7.2 variable templates (/showthread.php?tid=1742)



Rule 6.7.2 variable templates - gdavis - 24-07-2025

Is the following considered to be compliant:

Code:
template <typename T>
T var = 42;


By itself it seems not to be in the spirit of the rule since there's no "const" in the declaration.  But what if the only instantiation is something like:

Code:
int foo() { return var<const int>; }

Thank you.


RE: Rule 6.7.2 variable templates - misra cpp - 01-08-2025

In the introduction to the rules, there's the requirement:

"3.7.3 Templates
Unless otherwise stated, the guidelines shall only be applied to fully instantiated templates"

For a template variable, each instantiation has to be considered separately. So your example with <const int> is compliant, but if it were replaced by <int> it would be non-compliant