Date: Mon, 2 Nov 2020 09:05:46 -0500
> Can I use reflection in incomplete types ?
>
> struct X {
> int a;
> bool b;
> char c;
>
> constexpr variant < typename( members_of( reflexpr(X),
> meta::is_nonstatic_data_member) ) >
> get_by_name(std::string s) {
> constexpr auto members = members_of( reflexpr(X),
> meta::is_nonstatic_data_member);
> for constexpr (meta::info member : members) {
> if ( name_of( member ) == s ) {
> auto m = typename(member);
> return (this->*m);
> }
> }
> }
> } x ;
>
> This is a "get_by_name" sample where we can use dynamically
> x.get_by_name("a") as same as x.a;
>
Yes and no. You can write a metaprogarm to search for members by name, so
the code above should be fine. (We should probably provide a library API
for this.)
However, you cannot use lookup in the sense of [basic.lookup]. This should
fail:
struct X {
int a;
consteval {
auto info = reflexpr(X::a); // error: X is incomplete.
}
};
Andrew
>
> struct X {
> int a;
> bool b;
> char c;
>
> constexpr variant < typename( members_of( reflexpr(X),
> meta::is_nonstatic_data_member) ) >
> get_by_name(std::string s) {
> constexpr auto members = members_of( reflexpr(X),
> meta::is_nonstatic_data_member);
> for constexpr (meta::info member : members) {
> if ( name_of( member ) == s ) {
> auto m = typename(member);
> return (this->*m);
> }
> }
> }
> } x ;
>
> This is a "get_by_name" sample where we can use dynamically
> x.get_by_name("a") as same as x.a;
>
Yes and no. You can write a metaprogarm to search for members by name, so
the code above should be fine. (We should probably provide a library API
for this.)
However, you cannot use lookup in the sense of [basic.lookup]. This should
fail:
struct X {
int a;
consteval {
auto info = reflexpr(X::a); // error: X is incomplete.
}
};
Andrew
Received on 2020-11-02 08:06:00