AutosarC++19_03-A8.4.8¶
Output parameters shall not be used
Required inputs: IR
| Level | required |
|---|---|
| Target | design |
| Enforcement | automated |
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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_in_out_parameters¶
allow_in_out_parameters : bool = False
allow_modifying_calls¶
allow_modifying_calls : bool = True
allow_move_parameters¶
allow_move_parameters : bool = True
Note: move-constructors and move-assignments are allowed in either case.
allow_this_modification¶
allow_this_modification : bool = True
exclude_constructors¶
exclude_constructors : bool = False
exclude_operators¶
exclude_operators : bool = False
Note: if set to true, this also sets exclude_shift_operators to true.
exclude_shift_operators¶
exclude_shift_operators : bool = False
operator<<(), operator<<=(),
operator>>(), and operator>>=()).
only_report_multiple_output_parameters¶
only_report_multiple_output_parameters : bool = False
record_field_modification_threshold¶
record_field_modification_threshold : int = 0
0 : every field modification counts as an output parameter
100: the record pointer is only regarded an output parameter if all field are modified