HoooMaC / KareeemV2

Apache License 2.0
1 stars 0 forks source link

Making reference wrapper class #16

Open HoooMaC opened 1 year ago

HoooMaC commented 1 year ago

also include the constructor for share pointer and checking if the reference is valid

HoooMaC commented 1 year ago

possible implementation from cppreference ` namespace detail { template constexpr T& FUN(T& t) noexcept { return t; } template void FUN(T&&) = delete; }

template class reference_wrapper { public: // types using type = T;

// construct/copy/destroy
template<class U, class = decltype(
    detail::FUN<T>([std::declval](http://en.cppreference.com/w/cpp/utility/declval)<U>()),
    [std::enable_if_t](http://en.cppreference.com/w/cpp/types/enable_if)<![std::is_same_v](http://en.cppreference.com/w/cpp/types/is_same)<reference_wrapper, [std::remove_cvref_t](http://en.cppreference.com/w/cpp/types/remove_cvref)<U>>>()
)>
constexpr reference_wrapper(U&& u)
    noexcept(noexcept(detail::FUN<T>([std::forward](http://en.cppreference.com/w/cpp/utility/forward)<U>(u))))
    : _ptr([std::addressof](http://en.cppreference.com/w/cpp/memory/addressof)(detail::FUN<T>([std::forward](http://en.cppreference.com/w/cpp/utility/forward)<U>(u)))) {}

reference_wrapper(const reference_wrapper&) noexcept = default;

// assignment
reference_wrapper& operator=(const reference_wrapper& x) noexcept = default;

// access
constexpr operator T& () const noexcept { return *_ptr; }
constexpr T& get() const noexcept { return *_ptr; }

template<class... ArgTypes>
constexpr [std::invoke_result_t](http://en.cppreference.com/w/cpp/types/result_of)<T&, ArgTypes...>
    operator() (ArgTypes&&... args ) const
        noexcept([std::is_nothrow_invocable_v](http://en.cppreference.com/w/cpp/types/is_invocable)<T&, ArgTypes...>)
{
    return [std::invoke](http://en.cppreference.com/w/cpp/utility/functional/invoke)(get(), [std::forward](http://en.cppreference.com/w/cpp/utility/forward)<ArgTypes>(args)...);
}

private: T* _ptr; };

// deduction guides template reference_wrapper(T&) -> reference_wrapper; `