AutosarC++19_03-A8.4.8

Output parameters shall not be used

Required inputs: IR

Rationale

Output parameters are passed to a function as non-const references or pointers that can denote either in-out or out-only parameter. Using return value prevents from possible misuse of such a parameter.

Note: Prefer returning non-value types (i.e. types in a inheritance hierarchy) as std::shared_ptr or std::unique_ptr.

Example
// $Id: A8-4-8.cpp 306164 2018-02-01 15:04:53Z christof.meerwald $

    #include <iostream>
    #include <vector>
    // Compliant, return value
    std::vector<int> SortOutOfPlace(const std::vector<int>& inVec);

    // Non-compliant: return value as an out-parameter
    void FindAll(const std::vector<int>& inVec, std::vector<int>& outVec);

    struct B
    {
    };

    struct BB
    {
        B GetB() const& { return obj; }
        B&& GetB() && { return std::move(obj); }

         B obj;
    };

    // Non-compliant: returns a dangling reference
    BB&& MakeBb1()
    {
        return std::move(BB());
    }

    // Compliant: uses compiler copy-ellision
    BB MakeBb2()
    {
        return BB();
    }

    int main()
    {
        BB x = MakeBb2();

         auto cpd = x.GetB();            // copied value
         auto mvd = MakeBb2().GetB();    // moved value

         return 0;
    }
See Also
  • C++ Core Guidelines [11]: F.20: For ”out” output values, prefer return values to output parameters.

Possible Messages

Key

Text

Severity

Disabled

output_parameter

Don’t use output parameters.

None

False

Options

allow_in_out_parameters

allow_in_out_parameters : bool = False

Whether parameters used as in-out parameters rather than purely output parameters should be allowed.
 

allow_modifying_calls

allow_modifying_calls : bool = True

Whether a function may call a function on a parameter that modifies it, without regarding it as a violation here.
 

allow_move_parameters

allow_move_parameters : bool = True

Whether a move parameter (a rvalue reference) may be written to without triggering a violation.

Note: move-constructors and move-assignments are allowed in either case.

 

allow_this_modification

allow_this_modification : bool = True

Whether a function may call a member-function on a parameter that modifies this, that is the internal parameter state (allow_modifying_calls must be false if this is set to false).
 

exclude_constructors

exclude_constructors : bool = False

Whether not to report parameters on constructors.
 

exclude_operators

exclude_operators : bool = False

Whether not to report parameters on operator functions.

Note: if set to true, this also sets exclude_shift_operators to true.

 

exclude_shift_operators

exclude_shift_operators : bool = False

Whether not to report parameters on shift operator functions (operator<<(), operator<<=(), operator>>(), and operator>>=()).
 

only_report_multiple_output_parameters

only_report_multiple_output_parameters : bool = False

Whether to only report output-parameters, if a function has more than one or also returns a value.
 

record_field_modification_threshold

record_field_modification_threshold : int = 0

A percentage of how many record fields may be modified, before the record pointer is considered an output parameter:

0 : every field modification counts as an output parameter
100: the record pointer is only regarded an output parameter if all field are modified