|
1 | 1 | # Aliases
|
2 | 2 |
|
| 3 | +Aliases are pronounced **"synonym for**, and written using the same **name `:` kind `=` value** [declaration syntax](../cpp2/declarations.md) as everything in Cpp2: |
| 4 | + |
| 5 | +- **name** is declared to be a synonym for **value**. |
| 6 | + |
| 7 | +- **kind** can be any of the kinds: `namespace`, `type`, a function signature, or a type. |
| 8 | + |
| 9 | +- **`==`**, pronounced **"defined as a synonym for"**, always precedes the value. The `==` syntax stresses that during compilation every use of the name could be equivalently replaced with the value. |
| 10 | + |
| 11 | +- **value** is the expression that the **name** is a synonym for. |
| 12 | + |
| 13 | + |
3 | 14 | ## <a id="namespace-aliases"></a> Namespace aliases
|
4 | 15 |
|
5 |
| -TODO |
| 16 | +A namespace alias is written the same way as a [namespace](namespaces.md), but using `==` and with the name of another namespace as its value. For example: |
| 17 | + |
| 18 | +``` cpp title="Namespace aliases" hl_lines="1 2 4 5 8 12 16" |
| 19 | +// 'chr' is a namespace defined as a synonym for 'std::chrono' |
| 20 | +chr : namespace == std::chrono; |
| 21 | + |
| 22 | +// 'chrlit' is a namespace defined as a synonym for 'std::chrono_literals' |
| 23 | +chrlit : namespace == std::chrono_literals; |
| 24 | + |
| 25 | +main: () = { |
| 26 | + using namespace chrlit; |
| 27 | + |
| 28 | + // The next two lines are equivalent |
| 29 | + std::cout << "1s is (std::chrono::nanoseconds(1s).count())$ns\n"; |
| 30 | + std::cout << "1s is (chr::nanoseconds(1s).count())$ns\n"; |
| 31 | +} |
| 32 | +// Prints: |
| 33 | +// 1s is 1000000000ns |
| 34 | +// 1s is 1000000000ns |
| 35 | +``` |
| 36 | + |
6 | 37 |
|
7 | 38 | ## <a id="type-aliases"></a> Type aliases
|
8 | 39 |
|
9 |
| -TODO |
| 40 | +A namespace alias is written the same way as a [type](types.md), but using `==` and with the name of another type as its value. For example: |
| 41 | + |
| 42 | +``` cpp title="Type aliases" hl_lines="1 2 7 10" |
| 43 | +// 'imap<T>' is a type defined as a synonym for 'std::map<i32, T>' |
| 44 | +imap : <T> type == std::map<i32, T>; |
| 45 | + |
| 46 | +main: () = { |
| 47 | + // The next two lines declare two objects with identical type |
| 48 | + map1: std::map<i32, std::string> = (); |
| 49 | + map2: imap<std::string> = (); |
| 50 | + |
| 51 | + // Assertion they are the same type, using the same_as concept |
| 52 | + assert( std::same_as< decltype(map1), decltype(map2) > ); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
10 | 56 |
|
11 | 57 | ## <a id="function-aliases"></a> Function aliases
|
12 | 58 |
|
13 |
| -TODO |
| 59 | +A function alias is written the same way as a [function](functions.md), but using `==` and with a side-effect-free body as its value; the body must always return the same value for the same input arguments. For example: |
| 60 | + |
| 61 | +``` cpp title="Function aliases" hl_lines="1 2 6 9 12 15" |
| 62 | +// 'square' is a function defined as a synonym for the value of 'i * i' |
| 63 | +square: (i: i32) -> _ == i * i; |
| 64 | + |
| 65 | +main: () = { |
| 66 | + // It can be used at compile time, with compile time values |
| 67 | + ints: std::array<i32, square(4)> = (); |
| 68 | + |
| 69 | + // Assertion that the size is the square of 4 |
| 70 | + assert( ints.size() == 16 ); |
| 71 | + |
| 72 | + // And if can be used at run time, with run time values |
| 73 | + std::cout << "the square of 4 is (square(4))$\n"; |
| 74 | +} |
| 75 | +// Prints: |
| 76 | +// the square of 4 is 16 |
| 77 | +``` |
| 78 | + |
| 79 | +> Note: A function alias is compiled to a Cpp1 `#!cpp constexpr` function. |
| 80 | +
|
14 | 81 |
|
15 | 82 | ## <a id="object-aliases"></a> Object aliases
|
16 | 83 |
|
17 |
| -TODO |
| 84 | +An object alias is written the same way as an [object](objects.md), but using `==` and with a side-effect-free value. For example: |
| 85 | + |
| 86 | +``` cpp title="Function aliases" hl_lines="1 2 5 6" |
| 87 | +// 'BufferSize' is an object defined as a synonym for the value 1'000'000 |
| 88 | +BufferSize: i32 == 1'000'000; |
| 89 | + |
| 90 | +main: () = { |
| 91 | + buf: std::array<std::byte, BufferSize> = (); |
| 92 | + assert( buf.size() == BufferSize ); |
| 93 | +} |
| 94 | +``` |
18 | 95 |
|
| 96 | +> Note: An object alias is compiled to a Cpp1 `#!cpp constexpr` object. |
19 | 97 |
|
0 commit comments