Trivial accessor and mutator functions should be inlined
Trivial accessor and mutator functions should be inlined.
Inlined functions avoid the run-time overhead of function calls but can result in code bloat. If an accessor (getter) or mutator (setter) method is trivial, code bloat is not an issue. You can inline these methods to avoid the unnecessary overhead of function calls. You can also avoid repeating several syntax elements inside and outside the class definition.
Methods defined inside classes are implicitly considered as inlined methods. You can
inline methods defined outside classes explicitly by using the inline
keyword.
To determine if a method is trivial, the checker uses this criteria:
An accessor method is trivial if it has no parameters and contains one
return statement that returns a non-static data member or a
reference to a non-static data member.
The return type of the method must exactly match or be a reference to the type of the data member.
A mutator method is trivial if it has a void return type, one
parameter, and contains one assignment statement that assigns the parameter to a
non-static data member.
The parameter type must exactly match or be a reference to the type of the data member.
The checker flags trivial accessor and mutator methods defined outside their
classes without the inline keyword.
The checker does not flag template methods or virtual methods.
If you expect a rule violation but do not see it, refer to the documentation for Polyspace® Bug Finder™ or Polyspace Bug Finder Server™.
| Group: Basic concepts |
| Category: Advisory, Automated |