C++11 introduced new forms of literals using modified syntax and semantics to provide User-Defined Literals (UDL) also known as Extensible Literals. While there was the ability to use them the standard library did not use any of them. In C++14, the commission added some standard literals. In this post, we explain user-defined literals operators and we explain some of these standard literals added in C++14.
What are the user defined literal operators in C++?
C++11 introduced new forms of literals using modified syntax and semantics in order to provide User-Defined Literals (UDL) also known as Extensible Literals. Using user-defined literals, user-defined classes can provide new literal syntax and they can be used with the operator ""
to combine values with conversion operators. Here below, we explain how to use user-defined literals in C++.
What are the standard user-defined literals in C++14?
In C++14, we have some standard user-defined literal operators that comes with standard library. These are literals for basic strings, for chrono types, and for complex number types.
We can access to these operators by:
- using namespace std::literals;
- using namespace std::string_literals;
- using namespace std::literals::string_literals;
C++14 adds the following standard literals below,
For the string types there is an operator”” s() for basic string,
- s :
std::basic_string
types for creating the various string types std::string, std::wstring, etc.
here how we can use it with auto,
Suffixes for std::chrono::duration
values,
- h : hour type for the
std::chrono::duration
time intervals - m : minute type for the
std::chrono::duration
time intervals - s : second type for the
std::chrono::duration
time intervals - ms : millisecond type for the
std::chrono::duration
time intervals - ns : nanosecond type for the
std::chrono::duration
time intervals - us : u.second type for the
std::chrono::duration
time intervals
here how we can use them with auto,
Suffixes for complex number literals,
- if : imaginary number for the
std::complex<float>
types - i : imaginary number for the
std::complex<double>
types - il : imaginary number for the
std::complex<long double>
types
here how we can use them with auto,
there are more definitions.
Is there a full example of how to use standard user-defined literals in C++14?
Here is a full example about standard user-defined literals in C++.
For more information about the standard user-defined literals, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3402.pdf
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.