Skip to content

Commit 8f3702f

Browse files
committed
Add "take" example
1 parent 4afe430 commit 8f3702f

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

include/lefticus/tools/forwarding_ref.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ template<typename T> struct [[nodiscard]] forwarding_ref
5050

5151
[[nodiscard]] explicit constexpr forwarding_ref(reference_type ref_ LIFETIMEBOUND) noexcept : ref{ &ref_ } {}
5252
[[nodiscard]] constexpr operator reference_type() noexcept LIFETIMEBOUND { return static_cast<reference_type>(*ref); }
53+
[[nodiscard]] constexpr reference_type take() noexcept LIFETIMEBOUND { return static_cast<reference_type>(*ref); }
5354

5455
private:
5556
pointer_type ref;

include/lefticus/tools/moving_ref.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct [[nodiscard]] moving_ref
5858
moving_ref(const T &) = delete ("this type is only to be used with rvalue reference parameters");
5959

6060
[[nodiscard]] constexpr operator reference_type() noexcept LIFETIMEBOUND { return static_cast<reference_type>(*ref); }
61+
[[nodiscard]] constexpr reference_type take() noexcept LIFETIMEBOUND { return static_cast<reference_type>(*ref); }
62+
6163

6264
private:
6365
pointer_type ref;

test/moving_forwarding_ref.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ template<typename T> constexpr auto can_construct_const()
7575
return requires(const T mct) { T{ lefticus::tools::moving_ref<T>{ std::move(mct) } }; };
7676
}
7777

78+
TEST_CASE("[forwarding_ref] take() behaves as expected")
79+
{
80+
[] () consteval {
81+
auto use_forwarding_ref = []<typename T>(lefticus::tools::forwarding_ref<T> data) {
82+
std::string(data.take());
83+
};
84+
85+
use_forwarding_ref(lefticus::tools::forwarding_ref{"Hello World"});
86+
}();
87+
}
88+
7889

7990
TEST_CASE("[forwarding_ref] behaves as expected")
8091
{

0 commit comments

Comments
 (0)