dimanche 22 décembre 2019

add_lvalue_reference implementation example

When looking for std::add_lvalue_reference on cppreference, one can see the following implementation example:

namespace detail {

template <class T>
struct type_identity { using type = T; }; // or use std::type_identity (since C++20)

template <class T>
auto try_add_lvalue_reference(int) -> type_identity<T&>;
template <class T>
auto try_add_lvalue_reference(...) -> type_identity<T>;

} // namespace detail

template <class T>
struct add_lvalue_reference : decltype(detail::try_add_lvalue_reference<T>(0)) {};

Why is the second try_add_lvalue_reference needed and in which cases would it be called in place of the first one?

Aucun commentaire:

Enregistrer un commentaire