C++ code with user-defined literals _px, _true, and _false.
https://files.techhub.social/media_attachments/files/111/170/981/453/308/701/original/904be8fc0d3249bd.png
Tip 25 of #TuesdayCodingTips - User-defined literals
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.
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.
076萌SNS is a social network, courtesy of 076. It runs on GNU social, version 2.0.2-beta0, available under the GNU Affero General Public License.
All 076萌SNS content and data are available under the Creative Commons Attribution 3.0 license.