samedi 18 novembre 2023

The implementation of add_rvalue_reference

Why does the result become an rvalue reference when deducing from a function returning type directly for a function type, while the implementation in bool_constant yields the correct type?

#include <type_traits>

using namespace std;

template <typename T>
T &&test_rvalue_reference(int);
template <typename T>
T test_rvalue_reference(...);

static_assert(is_same_v<decltype(test_rvalue_reference<void ()>(0)), add_rvalue_reference_t<void ()>>);     // error
static_assert(is_same_v<decltype(test_rvalue_reference<void ()>(0)), void (&)()>);      // OK, lvalue reference


template <typename, typename T>
struct select_second_type {
    using type = T;
};
template <typename T>
typename select_second_type<T &&, true_type>::type try_add_rvalue_reference(int);
template <typename T>
false_type try_add_rvalue_reference(...);

template <typename T, bool = decltype(try_add_rvalue_reference<T>(0))::value>
struct add_right_value_reference {
    using type = T &&;
};
template <typename T>
struct add_right_value_reference<T, false> {
    using type = T;
};

static_assert(is_same_v<typename add_right_value_reference<void ()>::type, add_rvalue_reference_t<void ()>>);       // OK
static_assert(is_same_v<typename add_right_value_reference<void ()>::type, void (&&)()>);       // OK, rvlaue reference

Aucun commentaire:

Enregistrer un commentaire