Commit 248b90f6 authored by Pranav Thulasiram Bhat's avatar Pranav Thulasiram Bhat Committed by Facebook GitHub Bot

Deduction guide for Async

Summary:
This diff implements a deduction guide for explicitly creating and returning Async objects in lambdas and functions with deduced return types.

This saves the user from having to specify often lengthy return types when annotating such functions.

Reviewed By: A5he

Differential Revision: D21770141

fbshipit-source-id: f80898ef6eeadee7c5a7168aaae4031e224a0935
parent 641a5030
...@@ -99,6 +99,15 @@ class [[nodiscard]] Async<void> { ...@@ -99,6 +99,15 @@ class [[nodiscard]] Async<void> {
Async operator=(Async&&) = delete; Async operator=(Async&&) = delete;
}; };
#if __cpp_deduction_guides >= 201703
/**
* Deduction guide to make it easier to construct and return Async objects.
* The guide doesn't permit constructing and returning by reference.
*/
template <typename T>
explicit Async(T)->Async<T>;
#endif
/** /**
* Function to retrieve the result from the Async wrapper * Function to retrieve the result from the Async wrapper
* A function calling await must return an Async wrapper itself * A function calling await must return an Async wrapper itself
......
...@@ -2800,3 +2800,33 @@ TEST(FiberManager, asyncTraits) { ...@@ -2800,3 +2800,33 @@ TEST(FiberManager, asyncTraits) {
static_assert( static_assert(
std::is_same<int&, async::async_inner_type_t<async::Async<int&>>>::value); std::is_same<int&, async::async_inner_type_t<async::Async<int&>>>::value);
} }
#if __cpp_deduction_guides >= 201703
TEST(FiberManager, asyncConstructorGuides) {
auto getLiteral = []() { return async::Async(1); };
// int&& -> int
static_assert(
std::is_same<int, async::async_inner_type_t<decltype(getLiteral())>>::
value);
int i = 0;
auto tryGetRef = [&]() { return async::Async(static_cast<int&>(i)); };
// int& -> int
static_assert(
std::is_same<int, async::async_inner_type_t<decltype(tryGetRef())>>::
value);
auto tryGetConstRef = [&]() {
return async::Async(static_cast<const int&>(i));
};
// const int& -> int
static_assert(
std::is_same<int, async::async_inner_type_t<decltype(tryGetConstRef())>>::
value);
// int& explicitly constructed
auto getRef = [&]() { return async::Async<int&>(i); };
static_assert(
std::is_same<int&, async::async_inner_type_t<decltype(getRef())>>::value);
}
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment