-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<utility>: Remove self swap check from pair. #4674
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
Merged
Stephan T. Lavavej (StephanTLavavej)
merged 13 commits into
microsoft:main
from
Andor233:master
May 21, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1272f51
<utility>: Remove self swap check from pair.
Andor233 8645c97
Code format
Andor233 e0c464e
Compatible with C++11
Andor233 63e474a
Use existed defs
Andor233 706bb57
Add test for cpp23
Andor233 fe0d85e
Format code and add tests\GH_004597_self_swap into test.lst
Andor233 c04a6a7
Remove unnecessary parentheses.
StephanTLavavej f1aeccc
Add newline.
StephanTLavavej 3e16611
Fix bug number, add bug title.
StephanTLavavej 43c8ad1
Direct-init swap_counters.
StephanTLavavej 41251c1
Separately assert conditions instead of ANDing them.
StephanTLavavej 31fd40a
Consistently `assert` that `pcnt_` is non-null.
StephanTLavavej 3e45a7d
Add 10 to the int values to make them distinctive.
StephanTLavavej 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
| 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,90 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <cassert> | ||
| #include <utility> | ||
|
|
||
| struct swap_counter { | ||
| unsigned int* pcnt_ = nullptr; | ||
|
|
||
| friend _CONSTEXPR20 void swap(swap_counter& lhs, swap_counter& rhs) noexcept { | ||
| assert(lhs.pcnt_ != nullptr); | ||
| assert(rhs.pcnt_ != nullptr); | ||
| std::swap(lhs.pcnt_, rhs.pcnt_); | ||
| ++*lhs.pcnt_; | ||
| ++*rhs.pcnt_; | ||
| } | ||
|
|
||
| _CONSTEXPR20 bool operator==(unsigned int x) const { | ||
| assert(pcnt_ != nullptr); | ||
| return *pcnt_ == x; | ||
|
StephanTLavavej marked this conversation as resolved.
|
||
| } | ||
| }; | ||
|
|
||
|
|
||
| // Test GH-4597 "<utility>: Side effects in self-swaps of pair are skipped" | ||
| _CONSTEXPR20 bool test_gh_4597() { | ||
| { | ||
| unsigned int cnt{}; | ||
| std::pair<swap_counter, int> pr{swap_counter{&cnt}, 10}; | ||
| pr.swap(pr); | ||
| assert(cnt == 2u); | ||
| assert(pr.first == 2u); | ||
| assert(pr.second == 10); | ||
| } | ||
|
|
||
| { | ||
| unsigned int cnt{}; | ||
| std::pair<swap_counter, int> p1{swap_counter{&cnt}, 10}; | ||
| std::pair<swap_counter, int> p2{swap_counter{&cnt}, 11}; | ||
| p1.swap(p2); | ||
| assert(cnt == 2u); | ||
| assert(p1.first == 2u); | ||
| assert(p1.second == 11); | ||
| assert(p2.first == 2u); | ||
| assert(p2.second == 10); | ||
| } | ||
|
|
||
| { | ||
| unsigned int c1{}; | ||
| unsigned int c2{2}; | ||
| std::pair<swap_counter, int> p1{swap_counter{&c1}, 11}; | ||
| std::pair<swap_counter, int> p2{swap_counter{&c2}, 13}; | ||
| p1.swap(p2); | ||
| assert(c1 == 1u); | ||
| assert(c2 == 3u); | ||
| assert(p1.first == 3u); | ||
| assert(p1.second == 13); | ||
| assert(p2.first == 1u); | ||
| assert(p2.second == 11); | ||
| } | ||
|
|
||
| #if _HAS_CXX23 | ||
| { | ||
| unsigned int c1{}; | ||
| unsigned int c2{2}; | ||
| int i1 = 11; | ||
| int i2 = 13; | ||
| swap_counter s1{&c1}; | ||
| swap_counter s2{&c2}; | ||
| const std::pair<swap_counter&, int&> p1{s1, i1}; | ||
| const std::pair<swap_counter&, int&> p2{s2, i2}; | ||
| p1.swap(p2); | ||
| assert(c1 == 1u); | ||
| assert(c2 == 3u); | ||
| assert(p1.first == 3u); | ||
| assert(p1.second == 13); | ||
| assert(p2.first == 1u); | ||
| assert(p2.second == 11); | ||
| } | ||
| #endif // _HAS_CXX23 | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
| #if _HAS_CXX20 | ||
| static_assert(test_gh_4597()); | ||
| #endif // _HAS_CXX20 | ||
| assert(test_gh_4597()); | ||
| } | ||
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.