Using namespaces with user-defined literals is a good practice to avoid naming conflicts and organize your code better. Here's how you can effectively use namespaces with user-defined literals:
You can define user-defined literals inside a namespace to prevent potential naming conflicts with other parts of your code or third-party libraries. For example, let's define some distance literals inside a distance_literals
 namespace:
#include <iostream>
namespace distance_literals {
float operator""_mm(long double val) {
return static_cast<float>(val / 1000);
}
float operator""_cm(long double val) {
return static_cast<float>(val / 100);
}
float operator""_m(long double val) {
return static_cast<float>(val);
}
float operator""_km(long double val) {
return static_cast<float>(val * 1000);
}
}
int main() {
using namespace distance_literals;
float distance = 5.0_km;
std::cout << distance << " meters";
}
5000 meters
To use the literals defined in a namespace, you need to include a using namespace
directive in your code. This makes the literals available in the current scope:
using namespace distance_literals;
float d = 3.0_m;
Alternatively, you can qualify the literal with the namespace:
float d = distance_literals::3.0_km;
Here’s an example with a custom Distance
type and namespace:
#include <iostream>
class Distance {
public:
Distance(float value) : value{value} {}
float value;
};
namespace distance_literals {
Distance operator""_meters(long double val) {
return Distance{static_cast<float>(val)};
}
Distance operator""_kilometers(long double val) {
return Distance{static_cast<float>(val * 1000)};
}
}
std::ostream& operator<<(std::ostream& os, Distance d) {
os << d.value << " meters";
return os;
}
int main() {
using namespace distance_literals;
Distance d = 3.0_kilometers;
std::cout << d;
}
3000 meters
By leveraging namespaces, you can write more modular, organized, and conflict-free code, making it easier to manage and extend.
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