AutosarC++18_10-A8.4.7¶
“in” parameters for “cheap to copy” types shall be passed by value
Required inputs: IR
| Level | required |
|---|---|
| Target | design |
| Enforcement | automated |
Rationale
Passing an argument by value documents that the argument won’t be modified.
Copying the value (instead of passing by reference to const) also ensures that no indirection is needed in the function body to access the value.
For the purpose of this rule, ”cheap to copy” is defined as a trivially copyable type that is no longer than two words (i.e. pointers).
Example
// $Id: A8-4-7.cpp 305588 2018-01-29 11:07:35Z michal.szczepankiewicz $
#include <cstdint>
#include <iostream>
#include <string>
// Compliant, pass by value
void output(std::uint32_t i)
{
std::cout << i << ’\n’;
}
// Non-Compliant, std::string is not trivially copyable
void output(std::string s)
{
std::cout << s << ’\n’;
}
struct A
{
std::uint32_t v1;
std::uint32_t v2;
};
// Non-Compliant, A is trivially copyable and no longer than two words
void output(const A &a)
{
std::cout << a.v1 << ", " << a.v2 << ’\n’;
}
See Also
- C++ Core Guidelines [11]: F.16: For ”in” parameters, pass cheaply-copied types by value and others by reference to const
- JSF December 2005 [8]: AV Rule 116: Small, concrete-type arguments (two or three words in size) should be passed by value if changes made to formal parameters should not be reflected in the calling function.
- JSF December 2005 [8]: AV Rule 117.1: An object should be passed as const T& if the function should not change the value of the object.
- A18-9-2 in section 6.18.9
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
input_parameter |
Pass input parameters by value |
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_basic_types¶
allow_basic_types : set[bauhaus.ir.LIR_Class_Name] = {'C_Char_Type', 'Void_Type'}
exclude_operator_functions¶
exclude_operator_functions : bool = False
size_threshold¶
size_threshold : int = 2
word_size¶
word_size : int | None = None