-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added type and namespace alias declarations, closes #273
An alias can be to a namespace, type, function, or object.
Aliases are always a non-mutable view of the original entity. This is natural for namespace and type and function aliases, since those kinds of entited can't be mutated, but it bears calling out for an object alias which behaves as `auto const&`.
The current syntax is like non-alias declarations, but with `==` to connote equality with an existing entity, instead of `=` which connotes setting the new entity's value. For example:
lit: namespace == ::std::literals;
pmr_vec: <T> type
== std::vector<T, std::pmr::polymorphic_allocator<T>>;
func :== some_original::inconvenient::function_name;
vec :== my_vector; // note: const&, aliases are never mutable
Note: Function aliases are subsumed under the object case, so you can't declare a function alias with an explicit signature. Rationale:
- if there's no need for a (repetitive) explicit signature,
the object case covers it
- if the parameters/returns must be exact (no conversions),
a pointer to function covers it
- if parameter/return conversions are allowed,
std::function covers it
But if we do learn about a reason to add support for function aliases having an explicit signature, and possibly with conversions to/from the parameter/return types, they'll be a natural fit here.- Loading branch information
Showing
12 changed files
with
465 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
|
|
||
| N: namespace = { | ||
| pmr_vec: <T> type == std::vector<T, std::pmr::polymorphic_allocator<T>>; | ||
| } | ||
|
|
||
| N1: namespace == N; | ||
|
|
||
| myclass: type = { | ||
| // Default to public | ||
| str: type == std::string; | ||
|
|
||
| private str2: type == std::string; | ||
| } | ||
|
|
||
| N3: namespace == ::std::literals; | ||
|
|
||
| myfunc: () = { | ||
| v: N1::pmr_vec<myclass::str> = ("xyzzy", "plugh"); | ||
|
|
||
| v2 :== v; | ||
|
|
||
| for v2 do :(s) = | ||
| std::cout << "(s)$\n"; | ||
| } | ||
|
|
||
| main: () = { | ||
| view: type == std::string_view; | ||
| N4: namespace == std::literals; | ||
|
|
||
| myfunc2 :== myfunc; | ||
| myfunc2(); | ||
| } |
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/clang-12/pure2-type-and-namespace-aliases.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| xyzzy | ||
| plugh |
Empty file.
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/gcc-10/pure2-type-and-namespace-aliases.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| xyzzy | ||
| plugh |
Empty file.
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/msvc-2022/pure2-type-and-namespace-aliases.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| xyzzy | ||
| plugh |
1 change: 1 addition & 0 deletions
1
regression-tests/test-results/msvc-2022/pure2-type-and-namespace-aliases.cpp.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pure2-type-and-namespace-aliases.cpp |
67 changes: 67 additions & 0 deletions
67
regression-tests/test-results/pure2-type-and-namespace-aliases.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
|
|
||
| #define CPP2_USE_MODULES Yes | ||
|
|
||
| //=== Cpp2 type declarations ==================================================== | ||
|
|
||
|
|
||
| #include "cpp2util.h" | ||
|
|
||
|
|
||
| #line 2 "pure2-type-and-namespace-aliases.cpp2" | ||
| namespace N { | ||
|
|
||
| } | ||
|
|
||
| #line 8 "pure2-type-and-namespace-aliases.cpp2" | ||
| class myclass; | ||
|
|
||
| //=== Cpp2 type definitions and function declarations =========================== | ||
|
|
||
|
|
||
| #line 2 "pure2-type-and-namespace-aliases.cpp2" | ||
| namespace N { | ||
| template<typename T> using pmr_vec = std::vector<T,std::pmr::polymorphic_allocator<T>>; | ||
| } | ||
|
|
||
| namespace N1 = N; | ||
|
|
||
| class myclass { | ||
| // Default to public | ||
| public: using str = std::string; | ||
|
|
||
| private: using str2 = std::string; | ||
| }; | ||
|
|
||
| namespace N3 = ::std::literals; | ||
|
|
||
| auto myfunc() -> void; | ||
|
|
||
| #line 26 "pure2-type-and-namespace-aliases.cpp2" | ||
| auto main() -> int; | ||
|
|
||
| //=== Cpp2 function definitions ================================================= | ||
|
|
||
|
|
||
| #line 2 "pure2-type-and-namespace-aliases.cpp2" | ||
| namespace N { | ||
|
|
||
| } | ||
|
|
||
| #line 17 "pure2-type-and-namespace-aliases.cpp2" | ||
| auto myfunc() -> void{ | ||
| N1::pmr_vec<myclass::str> v {"xyzzy", "plugh"}; | ||
|
|
||
| auto const& v2 = std::move(v); | ||
|
|
||
| for ( auto const& cpp2_range = v2; auto const& s : cpp2_range ) | ||
| std::cout << cpp2::to_string(s) + "\n"; | ||
| } | ||
|
|
||
| auto main() -> int{ | ||
| using view = std::string_view; | ||
| namespace N4 = std::literals; | ||
|
|
||
| auto const& myfunc2 = myfunc; | ||
| myfunc2(); | ||
| } | ||
|
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-type-and-namespace-aliases.cpp2.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pure2-type-and-namespace-aliases.cpp2... ok (all Cpp2, passes safety checks) | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.