AUTOSAR C++14 Rule A8-2-1
When declaring function templates, the trailing return type syntax shall be used if the return type depends on the type of parameters
Since R2020a
Description
Rule Definition
When declaring function templates, the trailing return type syntax shall be used if the return type depends on the type of parameters.
Rationale
When the return type of a template depends on the types of parameters, using the trailing return type syntax improves readability of the code significantly.
For instance, for out-of-class definitions of methods, using the trailing return type
syntax means that you do not have to use the fully qualified return type of a function along
with the typename
keyword. Instead of explicitly specifying the fully
qualified return type for aMethod
in this
example:
template <typename T> class aClass { public: using vectorType = std::vector<T>; vectorType aMethod(T const&); }; //Difficult-to-read method definition //Part in bold indicates fully qualified return type of method template <typename T> typename aClass<T>::vectorType aClass<T>::aMethod(T const &) { };
template <typename T> class aClass { public: using vectorType = std::vector<T>; vectorType aMethod(T const&); }; template <typename T> auto aClass<T>::aMethod(T const &) -> vectorType { };
Polyspace Implementation
The checker flags function template declarations where the explicitly specified return type of a template function has the same scope as the template function itself.
For instance, in the preceding example, the function aMethod
has a
return type vectorType
, which has the same scope as
aMethod
, namely the class aClass<T>
. Instead of
explicitly specifying the fully qualified return type, you can use the trailing return type
syntax.
Because C++14 has enabled return-type deduction, you can use the auto
keyword to declare generic templates while omitting the trailing return type. In such cases,
Polyspace® does not raise a violation.
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
Check Information
Group: Declarators |
Category: Required, Automated |
Version History
Introduced in R2020a