Handling negative values in user-defined literals requires understanding the precedence rules in C++.
The key point is that the negation operator (-
) has lower precedence than user-defined literals. This means the literal function is called with a positive value first, and then the result is negated.
Here’s an example demonstrating this behavior with a custom Distance
 type:
#include <iostream>
class Distance {
public:
Distance(float value) : value{value} {}
Distance operator-() const {
return Distance{-value};
}
float value;
};
std::ostream& operator<<(
std::ostream& os, Distance d) {
os << d.value << " meters\n";
return os;
}
Distance operator""_meters(long double val) {
return Distance{static_cast<float>(val)};
}
int main() {
Distance d1 = -5.0_meters;
std::cout << d1;
}
-5 meters
The above code works because:
5.0_meters
calls operator""_meters
with 5.0
as an argument.Distance
object with a value of 5.0
.-
) is then applied to this Distance
object, invoking the overloaded operator-
function.If you need to handle negative values within the literal itself, consider implementing appropriate logic within the literal function. However, this is uncommon and usually unnecessary due to the precedence rules.
Here’s an example with integer literals:
#include <iostream>
int operator""_km(unsigned long long val) {
return static_cast<int>(val * 1000);
}
int main() {
int distance = -3_km;
std::cout << distance << " meters\n";
}
-3000 meters
Handling negative values in user-defined literals relies on understanding operator precedence.
The literal function processes the positive value first, and then the result can be negated. By correctly implementing and overloading the necessary operators, you can effectively manage negative values in your custom literals.
Answers to questions are automatically generated and may not have been reviewed.
A practical guide to user-defined literals in C++, which allow us to write more descriptive and expressive values