MISRA Discussion Forums
Rule 0-1-3/0-1-4/0-1-5 and (template) class members - 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.0 Language independent issues (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=129)
+---- Thread: Rule 0-1-3/0-1-4/0-1-5 and (template) class members (/showthread.php?tid=1309)



Rule 0-1-3/0-1-4/0-1-5 and (template) class members - grunwald - 01-02-2017

Do these rules apply to class members?

If so, do they apply to instances of template class members?

Code:
template
class C {
    T member1; // never used; compliant with 0-1-3?
    T member2; // C::member2 is used but C::member2 is not; compliant with 0-1-3?

    typedef T Unused; // typedef never used: non-compliant with 0-1-5.
    typedef T X; // C::X is used but C::X is not; compliant with 0-1-5?

    void call() { // only instantiated for C
       use(static_cast(member2));
    }
};

int main() {
  C().call();
  C();
}

The same question also applies to 0-1-4.

For 0-1-10 the situation is different, as class template member functions only get instantiated if they actually used. Thus C::call() is compliant with 0-1-10. A completely unused class template member function would be non-compliant with Rule 14-7-1.


Re: Rule 0-1-3/0-1-4/0-1-5 and (template) class members - misra cpp - 10-07-2017

The basic answer to all these queries is that every definition in the program must be used somewhere, or conversely, it should not be possible to remove a definition from the code and still have the program compile.

So for a template class, each member of the class must be used by at least one instantiation of the template.