veselink1 / refl-cpp

Static reflection for C++17 (compile-time enumeration, attributes, proxies, overloads, template functions, metaprogramming).
https://veselink1.github.io/refl-cpp/md__introduction.html
MIT License
1.06k stars 77 forks source link

Is there a way to access member type name? #15

Closed eliemichel closed 4 years ago

eliemichel commented 4 years ago

Is there a way to do something like this pseudo-code? I tried to read doc and code, but did not find.

for_each(refl::reflect(x).members, [&](auto member) {
    if constexpr (std::is_same<member.type, bool>) { // or member.typename == "bool"
        std::cout << "bool " << member.name << " = " << (member(x) ? "true" : "false") << ";";
    }
});
veselink1 commented 4 years ago

Hello,

Yes, there is.

for_each(refl::reflect(x).members, [&](auto member) {
    using member_type = decltype(member)::type;
    if constexpr (std::is_same_v<member_type, bool>) {
        std::cout << "bool" << member.name << " = " << (member(x) ? "true" : "false") << ";";
    }
});

You were pretty close to the solution. type is a typedef on decltype(member). Please, note that this is the cv-qualified type of the member.

eliemichel commented 4 years ago

Great, thanks!

eliemichel commented 4 years ago

(edit: I believe it is ::value_type rather than ::type)