-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Allow promises to be constructed for non-default-constructible types #2568
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 8 commits into
microsoft:main
from
MitalAshok:promise-not-default-constr
Mar 4, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
47e5e09
Allow promises to be constructed for non-default-constructible types
MitalAshok 507136b
Use if constexpr
MitalAshok 6c86ccd
Style changes
MitalAshok cbf9965
Created test file
MitalAshok d8b03e2
clang-tidy test
MitalAshok 21416b7
Test naming / include fixes
MitalAshok c418494
Merge branch 'main' into promise-not-default-constr
StephanTLavavej 1274a5b
Code review feedback.
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
4 changes: 4 additions & 0 deletions
4
tests/std/tests/GH_002488_promise_not_default_constructible_types/env.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,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\native_matrix.lst |
184 changes: 184 additions & 0 deletions
184
tests/std/tests/GH_002488_promise_not_default_constructible_types/test.cpp
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,184 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
|
StephanTLavavej marked this conversation as resolved.
|
||
| #include <atomic> | ||
| #include <cassert> | ||
| #include <exception> | ||
| #include <future> | ||
| #include <system_error> | ||
| #include <thread> | ||
|
|
||
| static std::atomic<int> has_default_objects{0}; | ||
| static std::atomic<int> no_default_objects{0}; | ||
| static std::atomic<int> no_default_or_assign_objects{0}; | ||
|
MitalAshok marked this conversation as resolved.
|
||
|
|
||
| struct has_default { | ||
| has_default() : x(0xbad) { | ||
| ++has_default_objects; | ||
| } | ||
| explicit has_default(int n) : x(n) { | ||
| ++has_default_objects; | ||
| } | ||
| has_default(const has_default& v) : x(v.x) { | ||
| ++has_default_objects; | ||
| } | ||
|
|
||
| ~has_default() { | ||
| --has_default_objects; | ||
| } | ||
|
|
||
| int x; | ||
| }; | ||
|
|
||
| struct no_default { | ||
| no_default() = delete; | ||
| explicit no_default(int n) : x(n) { | ||
| ++no_default_objects; | ||
| } | ||
| no_default(const no_default& v) : x(v.x) { | ||
| ++no_default_objects; | ||
| } | ||
|
|
||
| ~no_default() { | ||
| --no_default_objects; | ||
| } | ||
|
|
||
| int x; | ||
| }; | ||
|
|
||
| struct no_default_or_assign { | ||
| no_default_or_assign() = delete; | ||
| explicit no_default_or_assign(int n) : x(n) { | ||
| ++no_default_or_assign_objects; | ||
| } | ||
| no_default_or_assign(const no_default_or_assign& v) : x(v.x) { | ||
| ++no_default_or_assign_objects; | ||
| } | ||
|
|
||
| void operator=(const no_default_or_assign&) = delete; | ||
|
|
||
| ~no_default_or_assign() { | ||
| --no_default_or_assign_objects; | ||
| } | ||
|
|
||
| int x; | ||
| }; | ||
|
|
||
| template <class F> | ||
| void assert_throws_future_error(F f, std::error_code expected_code) { | ||
| try { | ||
| f(); | ||
| } catch (const std::future_error& e) { | ||
| assert(e.code() == expected_code); | ||
| return; | ||
| } catch (...) { | ||
| } | ||
| assert(false); | ||
| } | ||
|
|
||
| template <class T> | ||
| void run_tests() { | ||
| using Promise = std::promise<T>; | ||
| using Future = std::future<T>; | ||
|
|
||
| { | ||
| Promise p; | ||
| p.set_value(T(4)); | ||
| assert(p.get_future().get().x == 4); | ||
| } | ||
|
|
||
| { | ||
| Promise p; | ||
| Future f = p.get_future(); | ||
| T v(10); | ||
| p.set_value(v); | ||
| assert(f.get().x == 10); | ||
| assert_throws_future_error([&] { p.set_value(v); }, std::future_errc::promise_already_satisfied); | ||
| assert_throws_future_error([&] { f.get(); }, std::future_errc::no_state); | ||
| assert_throws_future_error([&] { p.get_future().get(); }, std::future_errc::future_already_retrieved); | ||
| } | ||
|
|
||
| { | ||
| Promise p; | ||
| Future f = p.get_future(); | ||
| p.set_exception(std::make_exception_ptr(5)); | ||
|
MitalAshok marked this conversation as resolved.
|
||
| try { | ||
| f.get(); | ||
| assert(false); | ||
| } catch (int i) { | ||
| assert(i == 5); | ||
| } catch (...) { | ||
| assert(false); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| Promise p; | ||
| Future f = p.get_future(); | ||
| p.set_exception(std::make_exception_ptr(3)); | ||
| assert_throws_future_error([&] { p.set_value(T(2)); }, std::future_errc::promise_already_satisfied); | ||
| try { | ||
| f.get(); | ||
| assert(false); | ||
| } catch (int i) { | ||
| assert(i == 3); | ||
| } catch (...) { | ||
| assert(false); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| Promise p; | ||
| Future f = p.get_future(); | ||
| std::atomic<int> failures{0}; | ||
| int succeeded = -1; | ||
| auto make_thread = [&](int n) { | ||
| return std::thread([&, n] { | ||
| try { | ||
| p.set_value(T(n)); | ||
| } catch (std::future_error) { | ||
| ++failures; | ||
| return; | ||
| } | ||
| succeeded = n; | ||
| }); | ||
| }; | ||
| std::thread threads[]{make_thread(0), make_thread(1), make_thread(2), make_thread(3), make_thread(4), | ||
| make_thread(5), make_thread(6), make_thread(7)}; | ||
|
|
||
| for (auto& t : threads) { | ||
| t.join(); | ||
| } | ||
|
|
||
| assert(failures == 7); | ||
| assert(succeeded != -1 && f.get().x == succeeded); | ||
| } | ||
|
|
||
| { | ||
| (void) std::async(std::launch::async, [] { return T(16); }); | ||
| (void) std::async(std::launch::async, [] { | ||
| const T x(40); | ||
| return x; | ||
| }); | ||
|
|
||
| Future f = std::async(std::launch::async, [] { return T(23); }); | ||
| assert(f.get().x == 23); | ||
| } | ||
|
|
||
| { | ||
| std::packaged_task<T()> pt([] { return T(7); }); | ||
| Future f = pt.get_future(); | ||
| pt(); | ||
|
|
||
| assert(f.get().x == 7); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| run_tests<has_default>(); | ||
| run_tests<no_default>(); | ||
| run_tests<no_default_or_assign>(); | ||
| assert(has_default_objects == 0); | ||
| assert(no_default_objects == 0); | ||
| assert(no_default_or_assign_objects == 0); | ||
| } | ||
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.