Skip to content

Commit

Permalink
Merge pull request #99 from jiixyj/reorder-constructor-constraints
Browse files Browse the repository at this point in the history
Modify constructor constraints to make 'optional<any>' work on libc++
  • Loading branch information
steve-downey authored Jan 6, 2025
2 parents bb60b34 + 7cb9b44 commit 4e8377f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/beman/optional26/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ struct hash<optional<T>>;

namespace detail {
template <class T, class U>
concept enable_forward_value = std::is_constructible_v<T, U&&> && !std::is_same_v<std::decay_t<U>, in_place_t> &&
!std::is_same_v<optional<T>, std::decay_t<U>>;
concept enable_forward_value = !std::is_same_v<std::decay_t<U>, optional<T>> &&
!std::is_same_v<std::decay_t<U>, in_place_t> && std::is_constructible_v<T, U&&>;

template <class T, class U, class Other>
concept enable_from_other =
std::is_constructible_v<T, Other> && !std::is_constructible_v<T, optional<U>&> &&
!std::is_same_v<T, U> && std::is_constructible_v<T, Other> && !std::is_constructible_v<T, optional<U>&> &&
!std::is_constructible_v<T, optional<U>&&> && !std::is_constructible_v<T, const optional<U>&> &&
!std::is_constructible_v<T, const optional<U>&&> && !std::is_convertible_v<optional<U>&, T> &&
!std::is_convertible_v<optional<U>&&, T> && !std::is_convertible_v<const optional<U>&, T> &&
Expand Down
11 changes: 11 additions & 0 deletions src/beman/optional26/tests/optional.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <ranges>
#include <tuple>
#include <algorithm>
#include <any>

#include <gtest/gtest.h>

Expand Down Expand Up @@ -1005,3 +1006,13 @@ TEST(OptionalTest, OptionalFromOptionalConstRefExplicit) {
beman::optional26::optional<copyable_from_const_lvalue_only> o5(std::move(o3));
ASSERT_TRUE(o5);
}

TEST(OptionalTest, OptionalOfAnyWorks) {
beman::optional26::optional<std::any> o1 = 42;

beman::optional26::optional<std::any> o2 = o1;
EXPECT_TRUE(std::any_cast<const int&>(*o2) == 42);

beman::optional26::optional<std::any> o3 = std::move(o1);
EXPECT_TRUE(std::any_cast<const int&>(*o3) == 42);
}

0 comments on commit 4e8377f

Please sign in to comment.