Skip to content

Commit 0ae88a1

Browse files
ddvampapolukhin
authored andcommitted
fix universal: use std::addressof in implementation of OptionalRef
Fixes #1208 Tests: протестировано CI Additionally, explicit `operator!=` has been removed. --- Pull Request resolved: #1209 Co-authored-by: antoshkka <antoshkka@userver.tech> Co-authored-by: antoshkka <antoshkka@userver.tech> commit_hash:d6ee51c8c3e53d4f7a599d1bb6a8cebf3c0c7451
1 parent a6205ef commit 0ae88a1

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

universal/include/userver/utils/optional_ref.hpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// @file userver/utils/optional_ref.hpp
44
/// @brief @copybrief utils::OptionalRef
55

6+
#include <memory>
67
#include <optional>
78
#include <type_traits>
89

@@ -35,7 +36,7 @@ class OptionalRef {
3536
constexpr OptionalRef(const OptionalRef&) noexcept = default;
3637
constexpr OptionalRef& operator=(const OptionalRef&) noexcept = delete;
3738

38-
constexpr OptionalRef(T& other) noexcept : data_(&other) {}
39+
constexpr OptionalRef(T& other) noexcept : data_(std::addressof(other)) {}
3940

4041
// Forming a reference to a temporary is forbidden
4142
explicit constexpr OptionalRef(const T&&) = delete;
@@ -101,12 +102,7 @@ class OptionalRef {
101102
"Attempt to initialize non-const T from a const optional value"
102103
);
103104

104-
if (!other) {
105-
return nullptr;
106-
}
107-
108-
auto& value = *other;
109-
return &value;
105+
return other.has_value() ? std::addressof(*other) : nullptr;
110106
}
111107

112108
T* const data_ = nullptr;
@@ -120,11 +116,6 @@ constexpr bool operator==(OptionalRef<T> lhs, OptionalRef<U> rhs) noexcept {
120116
return *lhs == *rhs;
121117
}
122118

123-
template <class T, class U>
124-
constexpr bool operator!=(OptionalRef<T> lhs, OptionalRef<U> rhs) noexcept {
125-
return !(lhs == rhs);
126-
}
127-
128119
} // namespace utils
129120

130121
USERVER_NAMESPACE_END

universal/src/utils/optional_ref_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include <gtest/gtest.h>
44

5+
#include <optional>
6+
7+
#include "overloaded_address_operator_test.hpp"
8+
59
USERVER_NAMESPACE_BEGIN
610

711
namespace {
@@ -29,6 +33,25 @@ TEST(OptionalRef, Constructions) {
2933
static_assert(!std::is_constructible_v<OptionalRef<short>, int&>);
3034
}
3135

36+
TEST(OptionalRef, ConstructionsAddressof) {
37+
{
38+
utils::OverloadedAddressOperator over;
39+
utils::OptionalRef<utils::OverloadedAddressOperator> ref(over);
40+
}
41+
{
42+
utils::OverloadedAddressOperator over{};
43+
utils::OptionalRef<utils::OverloadedAddressOperator> ref{over};
44+
}
45+
{
46+
std::optional<utils::OverloadedAddressOperator> over(std::in_place);
47+
utils::OptionalRef<utils::OverloadedAddressOperator> ref(over);
48+
}
49+
{
50+
std::optional<utils::OverloadedAddressOperator> over{std::in_place};
51+
utils::OptionalRef<utils::OverloadedAddressOperator> ref{over};
52+
}
53+
}
54+
3255
TEST(OptionalRef, Values) {
3356
int a1_val = 1;
3457
int b1_val = 1;

0 commit comments

Comments
 (0)