User Defined Literals

Overloading User-Defined Literals

Can user-defined literals be overloaded?

Abstract art representing computer programming

Yes, user-defined literals in C++ can be overloaded to handle different types of input. This allows you to create more versatile and expressive literals. Here’s how you can do it:

Overloading Based on Parameter Type

You can overload user-defined literals by defining multiple functions with different parameter types. The C++ standard supports the following parameter types for literals:

  • unsigned long long for integers
  • long double for floating-point numbers
  • const char* for strings
  • char for characters

Here’s an example of overloading user-defined literals for distance conversions:

#include <iostream>

class Distance {
 public:
  Distance(float value) : value{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)};
}

Distance operator""_meters(unsigned long long val) {
  return Distance{static_cast<float>(val)};
}

Distance operator""_kilometers(long double val) {
  return Distance{static_cast<float>(val * 1000)};
}

Distance operator""_kilometers(unsigned long long val) {
  return Distance{static_cast<float>(val * 1000)};
}

int main() {
  Distance d1 = 3.0_kilometers;
  Distance d2 = 1500_meters;

  std::cout << d1;
  std::cout << d2;
}
3000 meters
1500 meters

Benefits of Overloading

  • Versatility: You can handle different input types with the same literal suffix.
  • Readability: Overloading literals makes your code more readable and expressive.

Example: Strings and Numbers

Here’s another example demonstrating overloading with string and number literals:

#include <iostream>
#include <string>

std::string operator""_name(
  const char* str, size_t) {
  return std::string(str);
}

int operator""_age(unsigned long long val) {
  return static_cast<int>(val);
}

int main() {
  std::string playerName = "Legolas"_name;
  int playerAge = 2931_age;

  std::cout << "Player: " << playerName
    << ", Age: " << playerAge;
}
Player: Legolas, Age: 2931

Guidelines for Overloading

  • Consistent Naming: Use consistent and meaningful suffixes to avoid confusion.
  • Documentation: Clearly document the purpose of each overloaded version.
  • Testing: Test each overloaded function thoroughly to ensure they handle all expected input types correctly.

Conclusion

Overloading user-defined literals can enhance the expressiveness and versatility of your code. By following best practices and thoroughly testing your literals, you can create a powerful and intuitive API for your projects.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved