Tuesday Coding Tip 25 — User-defined literals

●5 ●29 ●93
calendar_today • schedule1 min read
— Originally published at medium.com

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


C++ has many built-in literals for disambiguating types of constants, such as u for unsigned, f for float, or time-related literals from the chrono header (like ms, h, or min).

The really cool thing is that you can define your own literals (since C++11) to make your domain-specific constants more legible. All these literals overload the operator“” with your custom _suffix. The data type that is supposed to be before _suffix is then determined by the function parameter type (consult the documentation for allowed parameter types: https://en.cppreference.com/w/cpp/language/user_literal ).

Thanks to compile-time computing, these literals don’t have to imply any runtime overhead for your code.

// Language-provided literals
const auto foo = 100u; // implies unsigned int
const auto bar = 10.f; // implies float instead of double

// Library-provided literals
#include <chrono>
using namespace std::literals;

const auto baz = "hello"s; // implies std::string instead of const char*
constexpr auto day = 14d; // If the value > 255, then the stored value is unspecified. 
constexpr auto twoDays = 48h;
constexpr auto week = 24h* 7;
constexpr auto my_duration_unit = 108ms;
constexpr auto theYear = 2021y;

An interesting use-case that I found is that literals can be leveraged to make described bool constants without having to wrap them in a temporary.

// Literal that describes a constant in pixel units
[[nodiscard]] consteval unsigned long long operator""_px(unsigned long long value) {
    return value;
}

const auto SCREEN_WIDTH = 1280_px;

#include <cstddef>

[[nodiscard]] consteval bool operator""_false(const char*, std::size_t) {
    return false;
}

[[nodiscard]] consteval bool operator""_true(const char*, std::size_t) {
    return true;
}

foo("isWindowOpen"_true, "isFullscreen"_false);

Want more #programming & #cpp deep dives?

Join 6,001+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 25 of 31 in Tuesday Coding Tips

1 Comment

0 votes

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Tuesday Coding Tip 27 — Aliasing and ‘using’ in C++

Jakub Neruda - Oct 20

Tuesday Coding Tip 17 — Wrapping C APIs

Jakub Neruda - Jun 30

Tuesday Coding Tip 31 — Deducing this

Jakub Neruda - Nov 17
chevron_left
2.1k Points • 127 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
43Posts
37Comments
22Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Commenters (This Week)

5 comments
4 comments

Contribute meaningful comments to climb the leaderboard and earn badges!