tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
reference_wrapper.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_FUNCTIONAL_REFERENCE_WRAPPER_HPP
4#define TETL_FUNCTIONAL_REFERENCE_WRAPPER_HPP
5
14
15namespace etl {
16
17namespace detail {
18template <typename T>
19constexpr auto FUN(T& t) noexcept -> T&
20{
21 return t; // NOLINT(bugprone-return-const-ref-from-parameter)
22}
23
24template <typename T>
25void FUN(T&&) = delete;
26
27} // namespace detail
28
39template <typename T>
41 using type = T;
42
55 template <
56 typename U,
57 typename
59 constexpr reference_wrapper(U&& u) noexcept(noexcept(detail::FUN<T>(etl::forward<U>(u))))
60 : _ptr(addressof(detail::FUN<T>(etl::forward<U>(u))))
61 {
62 }
63
66 constexpr reference_wrapper(reference_wrapper const& x) noexcept = default;
67
70 constexpr auto operator=(reference_wrapper const& x) noexcept -> reference_wrapper& = default;
71
73 [[nodiscard]] constexpr operator type&() const noexcept { return *_ptr; }
74
76 [[nodiscard]] constexpr auto get() const noexcept -> type& { return *_ptr; }
77
83 template <typename... Args>
84 constexpr auto operator()(Args&&... args) const noexcept(noexcept(invoke(get(), etl::forward<Args>(args)...)))
85 -> invoke_result_t<T&, Args...>
86 {
87 return invoke(get(), etl::forward<Args>(args)...);
88 }
89
90private:
91 type* _ptr;
92};
93
94// One deduction guide is provided for reference_wrapper to support
95// deduction of the sole class template parameter.
96template <typename T>
98
102template <typename T>
103[[nodiscard]] constexpr auto ref(T& t) noexcept -> reference_wrapper<T>
104{
105 return reference_wrapper<T>(t);
106}
107
111template <typename T>
112[[nodiscard]] constexpr auto ref(reference_wrapper<T> t) noexcept -> reference_wrapper<T>
113{
114 return ref(t.get());
115}
116
121template <typename T>
122[[nodiscard]] constexpr auto cref(T const& t) noexcept -> reference_wrapper<T const>
123{
125}
126
127template <typename T>
128[[nodiscard]] constexpr auto cref(reference_wrapper<T> t) noexcept -> reference_wrapper<T const>
129{
130 return cref(t.get());
131}
132
133template <typename T>
134void cref(T const&&) = delete;
135
136} // namespace etl
137
138#endif // TETL_FUNCTIONAL_REFERENCE_WRAPPER_HPP
Definition adjacent_find.hpp:8
constexpr auto cref(T const &t) noexcept -> reference_wrapper< T const >
Function templates ref and cref are helper functions that generate an object of type reference_wrappe...
Definition reference_wrapper.hpp:122
constexpr auto ref(T &t) noexcept -> reference_wrapper< T >
Function templates ref and cref are helper functions that generate an object of type reference_wrappe...
Definition reference_wrapper.hpp:103
reference_wrapper(T &) -> reference_wrapper< T >
constexpr auto addressof(T &arg) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:15
auto declval() noexcept -> add_rvalue_reference_t< T >
typename enable_if< B, T >::type enable_if_t
Definition enable_if.hpp:19
constexpr auto invoke(F &&f, Args &&... args) -> invoke_result_t< F, Args... >
Definition invoke.hpp:45
constexpr auto forward(remove_reference_t< T > &param) noexcept -> T &&
Forwards lvalues as either lvalues or as rvalues, depending on T. When t is a forwarding reference (a...
Definition forward.hpp:18
reference_wrapper is a class template that wraps a reference in a copyable, assignable object....
Definition reference_wrapper.hpp:40
constexpr auto operator=(reference_wrapper const &x) noexcept -> reference_wrapper &=default
Copy assignment operator. Drops the current reference and stores a reference to other....
constexpr reference_wrapper(reference_wrapper const &x) noexcept=default
Constructs a new reference wrapper. Copy constructor. Stores a reference to other....
constexpr reference_wrapper(U &&u) noexcept(noexcept(detail::FUN< T >(etl::forward< U >(u))))
Constructs a new reference wrapper. Converts x to T& as if by T& t = etl::forward(x);,...
Definition reference_wrapper.hpp:59
T type
Definition reference_wrapper.hpp:41
constexpr auto get() const noexcept -> type &
Returns the stored reference.
Definition reference_wrapper.hpp:76
constexpr auto operator()(Args &&... args) const noexcept(noexcept(invoke(get(), etl::forward< Args >(args)...))) -> invoke_result_t< T &, Args... >
Calls the Callable object, reference to which is stored. This function is available only if the store...
Definition reference_wrapper.hpp:84