CKTIS / C++
User-defined literals
A user-defined literal combines a numeric, character, or string literal with a custom suffix. Its literal operator turns that spelling into a value, such as an object representing a distance.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
constexpr ReturnType operator""_suffix(unsigned long long value);
constexpr ReturnType operator""_suffix(long double value);
123_suffix
1.5_suffixA user-defined literal, such as 42_m, is treated as a call to a literal operator or literal operator template. The suffix belongs to the literal's spelling; it is not an operation on a variable. The mechanism covers integers, floating-point numbers, characters, and strings; it does not redefine built-in suffixes such as LL.
A literal operator declaration uses a name such as operator""_m. The function belongs to a namespace, including the global namespace, and its declaration must be available to name lookup at the use site. Start custom suffixes with _ and avoid __: suffixes without an initial _ are reserved for future standardization, while those containing __ are reserved for implementations.
The numeric value-taking form requires unsigned long long for an integer literal or long double for a floating-point literal, not int or double. The raw numeric form accepts const char*; operator templates also exist. The ordinary string form takes a pointer and a std::size_t length excluding the terminating null, while the character form takes the corresponding character type.
In the example, 42_m calls operator""_m(42ULL) and constructs a Distance. constexpr enables the static_assert check; a suffix alone does not guarantee compile-time evaluation. The example targets verification in C++23 mode. The supplied document is an evolving working draft, not a frozen C++23 edition.
Examples
#include <iostream>
struct Distance {
unsigned long long metres;
};
constexpr Distance operator""_m(unsigned long long value) {
return Distance{value};
}
int main() {
constexpr auto distance = 42_m;
static_assert(distance.metres == 42);
std::cout << distance.metres << " m\n";
}
Result
42 m
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.