AUTOSAR C++14 Rule A8-4-7
"in" parameters for "cheap to copy" types shall be passed by value
Description
Rule Definition
"in" parameters for "cheap to copy" types shall be passed by value.
Rationale
You use an "in" parameter when you intend to only read that parameter within a function. If the parameter is cheap to copy, pass the parameter by value to:
Make it clear that you do not plan on modifying the parameter.
Avoid the additional indirection that is required to access the parameter from the function when you pass the parameter by reference.
A parameter is cheap to copy when both these conditions are true:
The parameter has a size less than or equal to two words. For instance, for a parameter
foo
,sizeof(foo) <= 2 * sizeof(int)
.The parameter is trivially copyable type. See is_trivially_copyable.
Polyspace Implementation
Polyspace® flags:
const
parameters that are passed by reference if the parameters are cheap to copy (sizeof <= 2 * sizeof(int)
and trivially copyable).const
parameters that are passed by value if the parameters are not cheap to copy. For instance, in this code snippet, both parametersstr
(expensive to copy) andb
(non-trivially copyable) are noncompliant.void func1(const std::string str); struct B { B(B const&) {} }; void func2(const B b);
Polyspace does not flag :
Non-
const
parameters that are passed by reference if those parameters are not cheap to copy and are not modified inside the function. Polyspace considers these parameters as "in" parameters."in" parameters that are passed by reference if those parameters are move-only types. For instance,
int f(const std::unique_ptr<int>& p);
.const
parameters that are passed by reference in copy constructors. For instance, no defect is raised onpoint
in this code snippet.class coord { public: coord(int x, int y) {p_x = x; p_y = y;} coord(const coord& point) { p_x = obj.p_x; p_y = obj.p_y;} //... private: int p_x, p_y; }; coord point{1, 1}; void func(const coord& point);
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 |