-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
180 additions
and
1 deletion.
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,34 @@ | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#ifndef BEMAN_ELIDE_DEDUCE_HPP | ||
#define BEMAN_ELIDE_DEDUCE_HPP | ||
|
||
#include <type_traits> | ||
#include <beman/elide/elide.hpp> | ||
|
||
namespace beman::elide { | ||
|
||
namespace detail::deduce { | ||
|
||
template<typename> | ||
struct is_elide : std::false_type {}; | ||
template<typename T, typename... Args> | ||
struct is_elide<beman::elide::elide<T, Args...>> : std::true_type {}; | ||
|
||
} | ||
|
||
template<typename T> | ||
struct deduce : std::remove_cvref<T> {}; | ||
template<typename T> | ||
requires | ||
detail::deduce::is_elide< | ||
std::remove_cvref_t<T>>::value && | ||
std::is_invocable_v<T> | ||
struct deduce<T> : std::invoke_result<T> {}; | ||
|
||
template<typename T> | ||
using deduce_t = typename deduce<T>::type; | ||
|
||
} // namespace beman::elide | ||
|
||
#endif // BEMAN_ELIDE_DEDUCE_HPP |
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
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
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,134 @@ | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <beman/elide/deduce.hpp> | ||
|
||
#include <type_traits> | ||
#include <utility> | ||
#include <beman/elide/elide.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace beman::elide { | ||
|
||
namespace { | ||
|
||
struct factory { | ||
int operator()(); | ||
}; | ||
|
||
struct no_lvalue_factory { | ||
int operator()() &&; | ||
}; | ||
|
||
struct split_factory { | ||
int operator()() &; | ||
double operator()() &&; | ||
}; | ||
|
||
template<typename T> | ||
struct wrapper { | ||
template<typename U> | ||
explicit wrapper(U&& u) noexcept( | ||
std::is_nothrow_constructible_v<T, U>) | ||
: t_(std::forward<U>(u)) | ||
{} | ||
constexpr const T& get() const noexcept { | ||
return t_; | ||
} | ||
private: | ||
T t_; | ||
}; | ||
|
||
template<typename T> | ||
explicit wrapper(T&&) -> wrapper<deduce_t<T>>; | ||
|
||
} | ||
|
||
static_assert(!detail::deduce::is_elide<int>::value); | ||
static_assert(detail::deduce::is_elide<elide<factory&>>::value); | ||
|
||
static_assert(std::is_same_v<int, deduce_t<int>>); | ||
static_assert(std::is_same_v<int, deduce_t<int&>>); | ||
static_assert(std::is_same_v<int, deduce_t<const int&>>); | ||
static_assert(std::is_same_v<int, deduce_t<int&&>>); | ||
static_assert(std::is_same_v<int, deduce_t<const int&&>>); | ||
|
||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
elide<factory&>>>); | ||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
elide<factory&>&>>); | ||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
const elide<factory&>&>>); | ||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
elide<factory&>&&>>); | ||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
const elide<factory&>&&>>); | ||
|
||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
elide<no_lvalue_factory&&>>>); | ||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
elide<no_lvalue_factory&&>&&>>); | ||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
const elide<no_lvalue_factory&&>&&>>); | ||
static_assert( | ||
std::is_same_v< | ||
elide<no_lvalue_factory&&>, | ||
deduce_t< | ||
const elide<no_lvalue_factory&&>&>>); | ||
static_assert( | ||
std::is_same_v< | ||
elide<no_lvalue_factory&&>, | ||
deduce_t< | ||
elide<no_lvalue_factory&&>&>>); | ||
|
||
static_assert( | ||
std::is_same_v< | ||
double, | ||
deduce_t< | ||
elide<split_factory&&>>>); | ||
static_assert( | ||
std::is_same_v< | ||
double, | ||
deduce_t< | ||
elide<split_factory&&>&&>>); | ||
static_assert( | ||
std::is_same_v< | ||
int, | ||
std::invoke_result_t< | ||
elide<split_factory&&>&>>); | ||
static_assert( | ||
std::is_same_v< | ||
int, | ||
deduce_t< | ||
elide<split_factory&&>&>>); | ||
|
||
TEST(Deduce, basic) { | ||
wrapper w(elide([]() noexcept { return 5; })); | ||
EXPECT_EQ(5, w.get()); | ||
} | ||
|
||
} |