Main Content

AUTOSAR C++14 Rule A14-7-1

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

Since R2021b

Description

Rule Definition

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

Rationale

A template can define operations on a generic type through one or more member variables or member functions. If the type that you use to instantiate the template does not provide all of the members that the template uses, your program might be ill-formed and might contain syntax or semantic errors.

For example, in the following code, template TmplClass declares a member function someProperty() but type myType does not. The instantiation of TmplClass by using myType is noncompliant and, as a result of the missing someProperty() function, inst.func(); causes a compilation error.

template <typename T>
class TmplClass
{
public:
    void func()
    {
        T t;
        t.someProperty();
    }
};
struct myType {
};

void Instance() noexcept
{
    TmplClass<myType> inst; //Non-compliant
// inst.func(); //compilation error, struct myType has no member function someProperty()
}

Polyspace Implementation

  • Polyspace® flags class, struct, or union template instantiations when the template parameter does not contain all of the members that the template uses.

    If you review results in the Polyspace desktop or web interfaces, in the template definition, the software highlights the members that are missing from the template parameter.

  • Polyspace does not flag:

    • Function template instantiations.

    • Template instantiations that use an incomplete type as the template parameter.

    • Template instantiations that use a template parameter, where the missing member is a member type (nested type) or a member template.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

class IncompleteType;
class MyClass
{

    struct MyStruct {
        int Field1;
        int Field2;
    };

    struct MyStructArray {
        int Field3[12];
    };

public:
    MyStruct property1;
    int property2[10];
    MyStruct property3[10];
    MyStructArray property4;


};

template <typename T>
class TemplClass
{
public:
    void fooA()
    {
        T t;

        t.property1 = {1, 2};
        t.property2[5] = 5;
        t.property3[2].Field1 = 6;
        t.property4.Field3[4] = 10;

    }
};

class MyType
{
public:
    int rank;
};

class MyOtherType
{
public:
    int property;
};

template <typename T1, typename T2>
class TemplClass2Param
{
    void func()
    {
        T1 t1;
        T2 t2;
        t1.rank = 5;
        t2.rank2 = 6;
    }
};

void instantiate(void)
{
    TemplClass<MyClass> var; // Compliant
    TemplClass<IncompleteType> varFromIncomplete; // Compliant
    TemplClass2Param<MyType, MyOtherType> otherVar; //Non-compliant

}

In this example, the first instantiation of template TmplCass by using template parameter MyClass is compliant because MyClass provides all the members that the template uses.

Polyspace does not flag the second instantiation of TmplCass that uses incomplete type IncompleteType.

The instantiation of TmplClass2Param is noncompliant because one of the template parameters (MyOtherType) does not provide a member variable rank2 that the template uses.

class MyClass
{
  public:
    int var;
    void foo(int);
    typedef int nestedtype; // member type  
    template<typename T>  
    void tmpl_func() { }  //member template
};

template <typename T>
class TmplClass {
  public:
    void func() {
        T t;
        int j = t.var;
        t.foo(j);
        typename T::otherNestedType newvar; //member type not in MyClass   
        newvar = 5;

        t.template other_tmpl_func<int>(); //member template not in MyClass     
    } 
};

void bar (void) {
    TmplClass<MyClass> instance;    // Compliant
}

In this example, template parameter MyClass does not have a member type otherNestedType or a member template other_tmpl_function, but the instantiation TmplClass<MyClass> instance is not flagged.

Check Information

Group: Templates
Category: Required, Automated

Version History

Introduced in R2021b