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,
1 2 3 |
auto str = "LearnCPlusPlus.org"s; // auto deduction to string |
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,
1 2 3 4 5 6 7 |
auto durh = 24h; // auto deduction to chrono::hours auto durm = 60min; // auto deduction to chrono::minutes auto durs = 120s; // auto deduction to chrono::seconds auto durms = 1000ms; // auto deduction to chrono::milliseconds auto durns = 2000ns; // auto deduction to chrono::nanoseconds |
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,
1 2 3 4 5 |
auto zi = 5i; // auto deduction to complex<double> auto zif = 7if; // auto deduction to complex<float> auto zil = 9il; // auto deduction to complex<long double> |
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++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <string> #include <iomanip> #include <complex> #include <chrono> using namespace std::literals; using namespace std::string_literals; // using namespace std::literals::string_literals; int main() { auto str = "LearnCPlusPlus.org"s; // auto deduction to string auto durh = 24h; // auto deduction to chrono::hours auto durm = 60min; // auto deduction to chrono::minutes auto durs = 120s; // auto deduction to chrono::seconds auto durms = 1000ms; // auto deduction to chrono::milliseconds auto durns = 2000ns; // auto deduction to chrono::nanoseconds auto zi = 5i; // auto deduction to complex<double> auto zif = 7if; // auto deduction to complex<float> auto zil = 9il; // auto deduction to complex<long double> } |
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.