Main Content

AUTOSAR C++14 Rule A8-4-9

"in-out" parameters declared as T & shall be modified

Since R2021a

Description

Rule Definition

"in-out" parameters declared as T & shall be modified.

Rationale

A function parameter meant to be both read and modified within a function is called an "in-out" parameter.

If you do not both read and modify a parameter, avoid passing by non-const reference so that the function prototype reflects the true nature of the parameter.

  • If you only read a parameter within a function, the parameter is actually an "in" parameter.

    Pass the parameter by const reference.

  • If you replace the entire contents of a parameter within a function, the parameter is actually an "out" parameter.

    If possible, avoid "out" parameters completely and store any output of the function in the function return value. See also AUTOSAR C++14 Rule A8-4-8.

Polyspace Implementation

The checker checks each function parameter passed by non-const reference and raises a violation if the parameter is only read within the function or its value completely replaced within the function.

The checker does not raise a violation if:

  • The parameter is an object and you access one or more of its data members, or invoke a non-const member function.

  • You pass a pointer or reference to the parameter on to another function.

  • The function is virtual. The reason is that even if the current function might not modify its parameter, an override of the function might modify its corresponding parameter.

  • The function is an unused class method.

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

#include <cstdint>
#include <vector>
#include <numeric>
#include <string>

int32_t addAllElements (std::vector<int32_t>& aVec) { //Noncompliant
     return std::accumulate(aVec.cbegin(), aVec.cend(), 0); 
}

int32_t addEveryElement (const std::vector<int32_t>& anotherVec) { //Compliant
     return std::accumulate(anotherVec.cbegin(), anotherVec.cend(), 0); 
}

In this example, the vector aVec is passed as a non-const reference to the function addAllElements. However, the vector is only read within the function and is only an "in" parameter, not an "in-out" parameter.

The function addEveryElement is a compliant version of the same function. The "in" parameter anotherVec is passed as a const reference.

#include <string>

void replaceString(std::string &Source, const std::string Replacement) { //Noncompliant
    if(Replacement.at(0)=='_')
         Source = Replacement; 
    else
         Source = "_null";
}

std::string replacementString(const std::string str) { //Compliant
    if(str.at(0)=='_')
         return str; 
    else
         return "_null";
}

In this example, the string Source is passed as a non-const reference to the function replaceString. However, the string is fully replaced within the function and is only an "out" parameter, not an "in-out" parameter.

The function replacementString is a compliant version of the same function, which also does not violate AUTOSAR C++14 Rule A8-4-8. The function has the same output as replaceString but stores the output in its return value.

Check Information

Group: Declarators
Category: Required, Automated

Version History

Introduced in R2021a