C++ Custom Literals
C++11 introduced user-defined literals, which allow you to define custom syntaxes for literal values. You can define your own literals for types like integers, floating-point numbers, strings, and more. Here's an example demonstrating the usage of custom literals:
#include <iostream>
constexpr long double operator"" _km(long double value) {
return value * 1000.0;
}
int main() {
long double distance = 5.5_km;
std::cout << "Distance: " << distance << " meters" << std::endl;
return 0;
}
In the above code, we define a user-defined literal operator operator"" _km that converts a value in kilometers to meters. The operator takes a long double argument representing the value in kilometers and returns the corresponding value
No comments:
Post a Comment