C++ Logo

SG7

Advanced search

Subject: Re: Incomplete types
From: Andrew Sutton (asutton.list_at_[hidden])
Date: 2020-11-02 08:05:46


> 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



SG7 list run by sg7-owner@lists.isocpp.org

Older Archives on Google Groups