Changes to user-defined literals

User-defined literals have always been a powerful feature in C++, allowing developers to define their own notation for literal values of custom types. This feature has been further enhanced in the upcoming C++23 standard with some important changes that make it even more flexible and expressive. In this blog post, we will explore the key changes to user-defined literals in C++23.

1. Expansion of Literal Operators

In C++23, the language allows us to define user-defined literal operators with parameters of any type. This means that we are no longer restricted to using only integral and floating-point types as the parameters. We can now use any type, including custom types, allowing for greater flexibility in designing our own literal syntax.

For example, assume we have a custom Color class:

class Color {
public:
    Color(int r, int g, int b) : red(r), green(g), blue(b) {}
    // ...
};

Color operator"" _color(const char* str, size_t size) {
    // Parse the string and return a Color object
    // ...
}

With C++23, we can now define literal operators that work with the Color type:

Color operator"" _colorRGBA(const char* str, size_t size) {
    // Parse the string and return a Color object with predefined alpha
    // ...
}

This expansion of literal operators provides developers with greater freedom to create expressive and natural syntax for their custom types.

2. UTF-8 String Literals

Another significant change in C++23 is the introduction of UTF-8 string literals. Previously, the standard only supported narrow and wide string literals, representing ASCII and wide characters, respectively. With the growing popularity of Unicode and the increasing need for working with non-ASCII characters, the addition of UTF-8 string literals is a welcome enhancement.

UTF-8 string literals are denoted by the u8 prefix, followed by the string literal:

auto myString = u8"Hello, 世界!";

This new feature ensures better support for internationalization and facilitates working with characters from a wide range of languages.

Conclusion

The changes to user-defined literals in C++23 significantly expand the possibilities for creating custom literal syntax. With the ability to define literal operators with parameters of any type and the introduction of UTF-8 string literals, developers have more flexibility and expressive power at their disposal. These updates further enhance the versatility of C++ and make it even more suitable for a wide variety of applications.

#C++ #UserDefinedLiterals