Skip to content

Commit 446d45f

Browse files
committed
Fix operator=(const opt::option<U>&) for reference options
1 parent 8921f0e commit 446d45f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

include/opt/option.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2973,7 +2973,7 @@ class OPTION_DECLSPEC_EMPTY_BASES OPTION_CONSUMABLE(unconsumed) option
29732973
constexpr option& operator=(const option<U>& other) {
29742974
if constexpr (std::is_reference_v<T>) {
29752975
if (other.has_value()) {
2976-
base::value = base::ref_to_ptr(static_cast<option<U>&&>(other.get()));
2976+
base::value = base::ref_to_ptr(other.get());
29772977
} else {
29782978
reset();
29792979
}

test/special.test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ TEST_CASE("reference") {
259259
}
260260
// NOLINTEND(misc-const-correctness)
261261
}
262+
SUBCASE("operator=") {
263+
int val = 1;
264+
opt::option<int&> a;
265+
const opt::option<std::reference_wrapper<int>> b{val};
266+
a = b;
267+
CHECK_EQ(a, val);
268+
CHECK_EQ(a.ptr_or_null(), &val);
269+
a = opt::option{std::ref(val)};
270+
CHECK_EQ(a, val);
271+
CHECK_EQ(a.ptr_or_null(), &val);
272+
}
262273
}
263274

264275
// NOLINTBEGIN(performance-move-const-arg,bugprone-use-after-move)

0 commit comments

Comments
 (0)