Skip to content

Commit b075656

Browse files
committed
add shared_ref::operator object_type&()
1 parent 8c6821b commit b075656

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/utki/shared_ref.hpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,24 @@ class shared_ref
160160
}
161161

162162
/**
163-
* @brief Get reference to the pointed type.
163+
* @brief Get reference to the object.
164164
*
165-
* @return Const reference to the pointed type.
165+
* @return Reference to the object.
166166
*/
167-
object_type& get() const noexcept
167+
constexpr object_type& get() const noexcept
168168
{
169169
ASSERT(this->p)
170170
return *this->p;
171171
}
172+
173+
/**
174+
* @brief Convert to the reference to the object.
175+
* @return Reference to the object.
176+
*/
177+
constexpr operator object_type&() const noexcept
178+
{
179+
return this->get();
180+
}
172181
};
173182

174183
/**

tests/unit/src/shared_ref.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ const tst::set set("shared_ref", [](tst::suite& suite) {
103103
tst::check_eq(etalon + "!", sr.get(), SL);
104104
});
105105

106+
suite.add("operator_convert_to_reference", []() {
107+
utki::shared_ref<std::string> sr = utki::make_shared<std::string>(etalon);
108+
109+
[](std::string& s) {
110+
tst::check_eq(s, etalon, SL);
111+
}(sr);
112+
113+
[](const std::string& s) {
114+
tst::check_eq(s, etalon, SL);
115+
}(sr);
116+
});
117+
118+
suite.add("operator_convert_to_reference_const", []() {
119+
utki::shared_ref<const std::string> sr = utki::make_shared<std::string>(etalon);
120+
121+
[](const std::string& s) {
122+
tst::check_eq(s, etalon, SL);
123+
}(sr);
124+
});
125+
106126
suite.add("const_autocast", []() {
107127
utki::shared_ref<const std::string> sr = utki::make_shared<std::string>(etalon);
108128

0 commit comments

Comments
 (0)