User Defined Literals (UDL) are added in C++ from C++11. Although, C++ provides literals for a variety of built-in types but these are limited.
Examples of literals for built-in types :
// Examples of classical literals for built-in types. 42 // int 2.4 // double 3.2F // float 'w' // char 32ULL // Unsigned long long 0xD0 // Hexadecimal unsigned "cd" // C-style string(const char")
Why do we use UDLs?
Let us consider below example to understand need of UDLs.
long double Weight = 2.3; // pounds? kilograms? grams? // With UDL, we attach units to the values which has // following advantages // 1) The code becomes readable. // 2) Conversion computations are done at compile time. weight = 2.3kg; ratio = 2.3kg/1.2lb;
To compute the above ratio it is necessary to convert them to same units. UDLs help us to overcome unit translation cost. We can define user-defined literals for user-defined types and new form of literals for built-in types. They help to make constants in code more readable. The value of UDLs is substituted with the actual value defined in the code by the compiler at compile time. UDL’s don’t save much of coding time but more and more calculations can be shifted to compile-time for faster execution.
Examples of User Defined Literals:
"hello"s // string 4.3i // imaginary 101000111101001b // binary 53h // hours 234093270497230409328432840923849 // extended-precision
UDLs are treated as a call to a literal operator. Only suffix form is supported. The name of the literal operator is operator “” followed by the suffix.
3600 3600.0023 16150 0.0646
z + y = (5.3,9) z * y = (-13.1,24.2) abs(z) = 5
Here, constexpr is used to enable compile time evaluation.
UDL can only work with the following parameters:
char const* unsigned long long long double char const*, std::size_t wchar_t const*, std::size_t char16_t const*, std::size_t char32_t const*, std::size_t
But return value can be of any types.
This article is contributed by Mahima Varshney. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Function Interposition in C with an example of user defined malloc()
- Multi-set for user defined data type
- User defined Data Types in C++
- How to create an unordered_map of user defined class in C++?
- How to create an unordered_set of user defined class or struct in C++?
- 2D Vector In C++ With User Defined Size
- How to implement user defined Shared Pointers in C++
- Type difference of character literals in C and C++
- What happen if we concatenate two string literals in C++?
- Types of Literals in C/C++ with Examples
- C++ set for user define data type
- C++ map having key as a user define data type
- Can we write a print statement within if parentheses?
- If memory allocation using new is failed in C++ then how it should be handled?
Improved By : Aman_1198