Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Literal operator declarations in C++

Declare custom suffixes `_KiB`, `_MiB`, and `_pct`. See how the literal's form selects an operator and how built-in `ULL` and unary minus differ from a custom suffix.

Version: ISO C++23; implementation support may vary

Syntax

namespace units {
    constexpr unsigned long long operator ""_KiB(unsigned long long n);
    constexpr unsigned long long operator ""_MiB(unsigned long long n);
    constexpr long double operator ""_pct(long double n);
}

// In the scope that uses the literals:
using units::operator ""_KiB;
using units::operator ""_MiB;
using units::operator ""_pct;
// Example expressions: 2_KiB, 1_MiB, -25.0_pct

An operator literal declaration, such as constexpr unsigned long long operator ""_KiB(unsigned long long n);, declares the function called by 2_KiB. Declare it at namespace scope and attach the suffix directly to the number. Lookup is unqualified: using units::operator ""_KiB; makes the declaration available where you write the literal. _KiB, _MiB, and _pct are custom suffixes, not standard-library suffixes. Start custom suffixes with _ and avoid __ anywhere in them.

A cooked numeric operator receives the value, not the source spelling. Integer forms prefer an unsigned long long parameter: 2_KiB is treated as operator ""_KiB(2ULL). ULL is a built-in integer suffix, not overloadable; don't stack it with _KiB. Floating-point forms prefer a long double parameter: 25.0_pct is treated as operator ""_pct(25.0L).

Without the matching cooked overload, lookup must find exactly one of two fallback forms: a raw operator taking const char* or a numeric literal operator template with a template<char...> pack. Both receive the source spelling rather than the numeric value. With only the long double overload, 25_pct is ill-formed: it has integer form and doesn't fall back to the floating-point overload by conversion.

In -25.0_pct, the minus is outside the literal: it acts on the returned value, not on the argument. In -2_KiB, the result is still unsigned long long and follows modulo arithmetic rather than becoming a negative byte count. For size calculations, check n <= max / factor before multiplying, where max is the result type's maximum; the example omits this check. The functions return numbers by value, and constexpr allows compile-time evaluation without requiring it for every call. User-defined literals date to C++11; this entry describes C++23, and implementation support may vary.

Examples

#include <iomanip>
#include <iostream>

namespace units {
constexpr unsigned long long operator ""_KiB(unsigned long long n) {
    return n * 1024ULL;
}

constexpr unsigned long long operator ""_MiB(unsigned long long n) {
    return n * 1048576ULL;
}

constexpr long double operator ""_pct(long double n) {
    return n / 100.0L;
}
}

int main() {
    using units::operator ""_KiB;
    using units::operator ""_MiB;
    using units::operator ""_pct;

    static_assert(2_KiB == 2048ULL);
    static_assert(1_MiB == 1048576ULL);
    static_assert(-25.0_pct < 0.0L);

    std::cout << 2_KiB << '\n'
              << 1_MiB << '\n'
              << std::fixed << std::setprecision(2) << -25.0_pct << '\n';
}

Result

2048
1048576
-0.25

The program prints the byte counts for `2_KiB` and `1_MiB`, then prints `-25.0_pct` as a fraction with two digits after the decimal point.

Related lessons

  • C++ · Lesson 60 · Advanced · Version: 3

Sources