AutosarC++17_03-A14.7.1

A type used as a template argument shall provide all members that are used by the template

Required inputs: IR

Rationale

If a type used as a template argument does not provide all the members used by the template, the instantiation of the template will result in an ill-formed program. It is not clear for developer whether the template should be used with the type.

Example
// $Id: A14-7-1.cpp 271696 2017-03-23 09:23:09Z piotr.tanski $
#include <cstdint>
class A {
public:
void setProperty(std::int32_t x) noexcept { property = x; }
void doSomething() noexcept {}

private:
std::int32_t property;
};
struct B {};
class C {
public:
void doSomething() noexcept {}
};
template <typename T> class D {
public:
void f1() {}
void f2() {
T t;
t.setProperty(0);
}
void f3() {
T t;
t.doSomething();
}
};

void fn() noexcept {
D<A> d1; // Compliant - struct A provides all needed members
d1.f1();
d1.f2();
d1.f3();

D<B> d2; // Non-compliant - struct B does not provide needed members
d2.f1();
// d2.f2(); // Compilation error - no ’property’ in struct B
// d2.f3(); // Compilation error - no member named ’doSomething’ in struct
// B

D<C> d3; // Non-compliant - struct C does not provide property
d3.f1();
// d3.f2(); // Compilation error - no property in struct C
d3.f3();
}
See Also
  • MISRA C++ 2008 [6]: Rule 14-7-2 (Required) For any given template specialization, an explicit instantiation of the template with the template arguments used in the specialization shall not render the program ill-formed.

Possible Messages

Key

Text

Severity

Disabled

missing_members

Type argument does not provide public member(s) {} used by the template

None

False

Options