-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement P3016R6 Resolve Inconsistencies In begin/end For valarray And Braced Initializer Lists #5847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vmichal
wants to merge
9
commits into
microsoft:main
Choose a base branch
from
vmichal:P3016R6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement P3016R6 Resolve Inconsistencies In begin/end For valarray And Braced Initializer Lists #5847
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d283bae
Add tests.
vmichal 498ee1c
Remove free function overloads for std::initializer_list
vmichal 497704e
Change begin/end for valarray from friend free functions to members.
vmichal 0887c8d
Add feature test macros __cpp_lib_initializer_list and __cpp_lib_vala…
vmichal 9a56b7e
Remove extra whitespace.
vmichal ecc409a
Fix incorrect formatting.
vmichal b9c607a
Update tests to unconditionally support P3016R6.
vmichal 1f660da
Correct placement of feature test macros in yvals_core.h
vmichal dcf8058
Add constexpr tests for std::initializer_list and iteration over brac…
vmichal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_matrix.lst |
This file contains hidden or 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,87 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <initializer_list> | ||
| #include <iterator> | ||
| #include <type_traits> | ||
| #include <valarray> | ||
|
|
||
| #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) | ||
|
|
||
| namespace my { | ||
|
|
||
| template <typename T> | ||
| void begin(std::initializer_list<T>); | ||
|
|
||
| template <typename T> | ||
| void end(std::initializer_list<T>); | ||
|
|
||
| template <typename T> | ||
| void empty(std::initializer_list<T>); | ||
|
|
||
| template <typename T> | ||
| void data(std::initializer_list<T>); | ||
| } // namespace my | ||
|
|
||
| _CONSTEXPR17 bool test_initializer_list() { | ||
| // Check that free functions in std still can be invoked on std::initializer_list | ||
| std::initializer_list<int> il = {1, 2, 3}; | ||
|
|
||
| using namespace std; | ||
| (void) begin(il); | ||
| (void) cbegin(il); | ||
| (void) end(il); | ||
| (void) cend(il); | ||
| (void) size(il); | ||
| (void) empty(il); | ||
| (void) data(il); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool test_valarray() { | ||
| // Check that free functions in std can be invoked on std::valarray | ||
| std::valarray<int> v{1}; | ||
|
|
||
| using namespace std; | ||
| (void) begin(v); | ||
| (void) cbegin(v); // Did not compile before P3016R6 | ||
| (void) end(v); | ||
| (void) cend(v); // Did not compile before P3016R6 | ||
| (void) size(v); | ||
| // There are no members 'empty' and 'data' of valarray | ||
| // (void) empty(il); | ||
| // (void) data(il); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| constexpr bool test_braced_init_list_iteration() { | ||
| int sum = 0; | ||
| for (int n : {1, 2, 3, 4}) { | ||
| sum += n; | ||
| } | ||
| return sum == 10; | ||
| } | ||
|
|
||
|
|
||
| int main() { | ||
| // Check that free functions in std can't be invoked with braced-initializer-list. | ||
| // If they could be invoked, the following expressions would be ambiguous between std:: and my:: | ||
| using namespace std; | ||
| using namespace my; | ||
| STATIC_ASSERT(std::is_same_v<decltype(begin({1, 2, 3})), void>); | ||
| STATIC_ASSERT(std::is_same_v<decltype(end({1, 2, 3})), void>); | ||
| STATIC_ASSERT(std::is_same_v<decltype(empty({1, 2, 3})), void>); | ||
| STATIC_ASSERT(std::is_same_v<decltype(data({1, 2, 3})), void>); | ||
|
|
||
| test_initializer_list(); | ||
| test_valarray(); | ||
| test_braced_init_list_iteration(); | ||
|
|
||
| #if _HAS_CXX17 | ||
| STATIC_ASSERT(test_initializer_list()); | ||
| #endif // _HAS_CXX17 | ||
| // STATIC_ASSERT(test_valarray()); // valarray is not literal | ||
| STATIC_ASSERT(test_braced_init_list_iteration()); | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For archaeology:
constexprforinitializer_listwas added to C++14 via WG21-N3471, whileconstexprfor freestd::begin/std::endwas added to C++17 via WG21-P0031R0.I'm a bit hesitant now as the changes would disallow executingfor (int n : {1, 2, 3}) { /* ... */ }in constant evaluation in C++14 mode, while WG21-N3471 intentionally allowed this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No language lawyer here, so correct me if I am wrong, but the range-based for loop does not use the free function in this case, but rather the member begin/end. Those are marked as constexpr unconditionally (not only in C++14 onwards) in code.
Link to cppinsights C++14 mode (very powerful argument for normal developers, not sure whether it is appropriate for STL development 😅 ) https://cppinsights.io/s/faddcfbd
Nevertheless, I have added the for loop to the test suite and it compiles and executes in all modes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you are right. Sorry. I must have been be confused with some
constexprreduction.