lundi 27 mars 2023

SFINAE for std::reference_wrapper constructors

The implementation of std::reference_wrapper in cppreference has the following constructor. Can someone explain what detail::FUN<T>(..) does or provide some detail as to what this SFINAE construct is trying to accomplish? Is this just to make sure U& is convertible to T&?

    namespace detail {
      template <class T> constexpr T& FUN(T& t) noexcept { return t; }
      template <class T> void FUN(T&&) = delete;
    }
    // construct/copy/destroy
    template <class U, class = decltype(
        detail::FUN<T>(std::declval<U>()),
        std::enable_if_t<!std::is_same_v<reference_wrapper, std::remove_cvref_t<U>>>()
    )>
    constexpr reference_wrapper(U&& u)
        noexcept(noexcept(detail::FUN<T>(std::forward<U>(u))))
        : _ptr(std::addressof(detail::FUN<T>(std::forward<U>(u)))) {}
 

Aucun commentaire:

Enregistrer un commentaire