AutosarC++17_03-A13.1.3¶
User defined literals operators shall only perform conversion of passed parameters
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
It is expected behavior that the user-defined literals operators are only used to convert passed parameters to the type of declared return value. User-defined literals are not supposed to provide any other side-effects.
Example
// $Id: A13-1-3.cpp 271927 2017-03-24 12:01:35Z piotr.tanski $
#include <cstdint>
#include <iostream>
struct Cube {
unsigned long long int volume;
constexpr explicit Cube(unsigned long long int v) : volume(v) {}
};
constexpr Cube operator"" _m3(unsigned long long int volume) {
return Cube(volume); // Compliant
}
struct Temperature {
unsigned long long int kelvins;
constexpr explicit Temperature(unsigned long long int k) : kelvins(k) {}
};
constexpr Temperature operator"" _K(unsigned long long int kelvins) {
return Temperature(kelvins); // Compliant
}
static void sumDistances(std::int32_t distance) {
static std::int32_t overallDistance = 0;
overallDistance += distance;
}
struct Distance {
long double kilometers;
explicit Distance(long double kms) : kilometers(kms) {}
};
Distance operator"" _m(long double meters) {
sumDistances(meters); // Non-compliant - function has a side-effect
return Distance(meters / 1000);
}
void operator"" _print(const char *str) {
std::cout << str << ’\n’; // Non-compliant - user-defined literal operator
// does not perform conversion and has a
// side-effect
}
See Also
- none
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
literal_side_effects |
User-defined literals shall not have side effects. |
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
This rule has no individual options.