TheNitesWhoSay / RareCpp

Creating a simpler, more intuitive means of C++ reflection
MIT License
124 stars 6 forks source link

How to check whether there exists a field of specific type? #94

Closed hooyuser closed 2 years ago

hooyuser commented 2 years ago

Given a specific type, say int, how to check whether there exists an integer field at compile time?

Or are there some ways to get a tuple of field types? So I can check types like this.

struct Pos {
    float x;
    int y;
    using MemberTypes = std::tuple<float, int>;
};

constexpr bool has_int_member = [] <typename... Ts> (std::tuple<Ts...>) {
       return (std::is_same_v<Ts, int> | ...);
}(Pos::MemberTypes{});

static_assert(has_int_member);
TheNitesWhoSay commented 2 years ago

Support for fold expressions coming soon! In the meantime, this trait works for your use case: https://godbolt.org/z/dab4o8nMK

hooyuser commented 2 years ago

Thank you for your quick reply. That works for me. Expect to see how folding expressions will work in the future!