|
| 1 | +# About |
| 2 | + |
| 3 | +Literals are fixed values that are used directly in code. |
| 4 | +They do not need any computation during run-time. |
| 5 | +They come in various forms to represent different types of data efficiently. |
| 6 | +The most frequent literals are of numeric nature, but there are also literal operators for date, time and string types. |
| 7 | +A later concept will show how user-defined literal operators can be used. |
| 8 | + |
| 9 | +## Numeric Literals |
| 10 | + |
| 11 | +Numeric literals include decimal, octal, hexadecimal, and binary representations. |
| 12 | +These basics were already covered in the [numbers concept][numbers-concept]. |
| 13 | + |
| 14 | +All representations like `23`, `0x50f7ba11` and `0xba5eba11` are understood as `int` by the compiler. |
| 15 | +The general `int` type is signed and ([depending on the compiler and the system][in-depth-integers]), often has a range of `-2'147'483'648` to `2'147'483'647`. |
| 16 | +This is sufficient most of the time and you don't need to worry about the specific type. |
| 17 | + |
| 18 | +Some use-cases demand larger ranges or very small memory footprints of programs. |
| 19 | +To be explicit one can directly state specific integer types with the numeric literal. |
| 20 | + |
| 21 | +When negative numbers aren't needed, but the added range is desired the `u` or `U` suffix are used for _unsigned_ integers. |
| 22 | +For greater ranges `L` or even `LL` can be used for long and long long types. |
| 23 | + |
| 24 | +~~~~exercism/caution |
| 25 | +The use of lower-case `l` is permitted, but it is easily confused with the number `1` and thus discouraged. |
| 26 | +~~~~ |
| 27 | + |
| 28 | +```cpp |
| 29 | +auto net_worth_christian_grey{2'500'000'000U}; // out of range for 32-bit integers |
| 30 | +auto net_worth_carlisle_cullen{46'000'000'000LL}; // int and uint are not enough |
| 31 | +``` |
| 32 | +
|
| 33 | +Floating-point numbers usually resolve to `double` during compilation. |
| 34 | +This is a good default case and use-cases with the narrower `float` type are less frequent than the unsigned example above. |
| 35 | +
|
| 36 | +```cpp |
| 37 | +auto light_year_in_m{9.46073e+15f}; // well in the range of float |
| 38 | +auto earth_to_edge_comoving_distance_in_nm{4.32355e+32}; // needs double type for magnitude |
| 39 | +auto eulers_number{2.718281828459045d}; // needs double type for precision |
| 40 | +``` |
| 41 | + |
| 42 | +~~~~exercism/advanced |
| 43 | +## Character and String Literals |
| 44 | +
|
| 45 | +Other concepts already used character literals with single quotes like `'}'` or `'@'`. |
| 46 | +
|
| 47 | +In C++ `char` is limited to the first 128 [ascii character codes][ascii-code]. |
| 48 | +To use several ascii chars or extended ascii characters like `‰` or even unicode characters like `麗` and `ẞ` other types are needed. |
| 49 | +In previous concept string literals were introduced with _double quotes_: `"一体どういう意味ですか。C++です"`. |
| 50 | +The actual type of this Japanese phrase is `const char (&)[46]`, a C-style string. |
| 51 | +
|
| 52 | +The use of string literals is not activated by default. |
| 53 | +To use the _string_ literal `""s` or the _string-view_ literal `""sv`, the user has to specify their use by using the related namespace: |
| 54 | +
|
| 55 | +```cpp |
| 56 | +#include <string> |
| 57 | +#include <string_view> |
| 58 | +
|
| 59 | +using namespace std::literals; |
| 60 | +
|
| 61 | +auto green_light{"무궁화 꽃 이 피었 습니다"}; |
| 62 | +// green_light type is const char (&)[36] |
| 63 | +auto umbrella{"달고나"s}; |
| 64 | +// umbrella type is std::basic_string<char>, the same as std::string |
| 65 | +auto il_nam{"보는 것이 하는 것보다 더 재미있을 수가 없지"sv}; |
| 66 | +// il_nam type is std::basic_string_view<char> |
| 67 | +``` |
| 68 | +
|
| 69 | +A _string_view_ can be seen as a reference to a const string. |
| 70 | +
|
| 71 | +~~~~ |
| 72 | + |
| 73 | +[numbers-concept]: https://exercism.org/tracks/cpp/concepts/numbers |
| 74 | +[in-depth-integers]: https://www.learncpp.com/cpp-tutorial/fixed-width-integers-and-size-t/ |
| 75 | +[ascii-code]: https://www.ascii-code.com/ |
0 commit comments