Casting in trailing reverse type causes SFINAE to crash

I re-performed boost::hana::is_validfor the purpose of the study. Use case:

struct Person {
    std::string name;
};

int main()
{
    auto has_name = is_valid([](auto&& t) -> decltype((void) t.name) {});

    Person jon{"snow"};
    static_assert(has_name(jon), "");
    static_assert(!has_name(1), "");
}

Implementation:

namespace detail {

template<typename F>
struct is_valid_impl {
    template<typename T, typename = std::result_of_t<F&&(T&&)>>
    constexpr bool operator()(T&&) const noexcept { return true; }

    constexpr bool operator()(...) const noexcept { return false; }
};

}  // namespace detail

template<typename F>
constexpr auto is_valid(F&&)
{
    return detail::is_valid_impl<F>{};
}

However, I do not know why the Hana user guide recommends casting the type of requested member to void(see here ); can't we just use decltype(t.name)instead decltype((void) t.name)?

In addition, the reduction to voidcauses tests fail in the GCC <5.3, while no transmission code works for GCC 5.1+. What could be the reason?

+4
source share
1 answer

, :

@snippet/tutorial/introspection.cpp non_static_member_from_object

, x.member void? , , .

+2

Source: https://habr.com/ru/post/1667643/


All Articles