Main Content

AUTOSAR C++14 Rule A14-1-1

A template should check if a specific template argument is suitable for this template

Since R2021b

Description

Rule Definition

A template should check if a specific template argument is suitable for this template.

Rationale

A template defines the operations of a class or function for generic template types. If these operations require that the template types have specific characteristics, for instance the data type must be copy constructible, check the template arguments to ensure that they are suitable and have the required characteristics. Typically, you use static_assert assertions to perform this check at compile time, for instance, static_assert(std::is_copy_constructible<T>).

Polyspace Implementation

Polyspace® flags template classes, structs, unions, and functions unless one of the following is true:

  • The template contains at least one static_assert assertion, even if that assertion does not test the characteristics of the template parameters.

  • The template is explicitly fully specialized even when it does not contain any static_assert assertions.

Polyspace does not flag template declarations.

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

In this example, templates specializedClass, func_def, and myStruct are noncompliant because these templates do not do not contains any static_assert statement and they are not fully specialized. Polyspace reports violations of this rule on the noncompliant templates.

Polyspace does not flag:

  • The class template myTemplate because it uses static_assert to check whether the template type is copy-constructible. If the instantiation of myTemplate<myClass> in function F() is uncommented, the assertion results in a compile-time error.

  • The struct template floatStruct because this template uses a static_assert statement that checks if the input type is float.

  • The class template specializedClass<std::int32_t> because it is explicitly fully specialized.

  • The function template declaration func_decl because the template is not implemented.

#include <cstdint>
#include <type_traits>

template<typename T>
class myTemplate  // Compliant. Use of static_assert
{

    static_assert(std::is_copy_constructible<T>(),
                  "Template type not copy constructible.");
    //...

};
template<typename T>
class specializedClass //Non-compliant
{
};

template<typename T>
struct myStruct //Non-compliant
{
	//...
};
template<typename T>
struct floatStruct //Compliant
{
	static_assert(std::is_floating_point<T>::value, "Expecting a floating point type");
};
template<>
class specializedClass<std::int32_t>  // Compliant. Explicit full specialization
{
};

class myClass  //Not a template
{
public:
    myClass() = default;
    myClass(myClass const&) = delete;

};

class myOtherClass  //Not a template
{

};

template<typename T>
void func_decl(T const& obj) noexcept(false); // Compliant. Template declaration


template <typename T>
void func_def(T const& obj) noexcept(false)  //Non-compliant
{


}



void F()
{

    //myTemplate<myClass> a; // myClass is not copy constructible. Compile-time error.
    myTemplate<myOtherClass> b; //myOtherClass is copy constructible.
}

Check Information

Group: Templates
Category: Advisory, Non-automated

Version History

Introduced in R2021b

expand all