lock3 / meta

122 stars 11 forks source link

meta::data_member_range not defined for generated struct #278

Closed rbock closed 3 years ago

rbock commented 3 years ago

Hi,

While trying to find a workaround for #276, I noticed that I cannot iterate over the data members of a generated struct:

template <typename... Ts> class tuple {
  consteval {
    constexpr std::array<meta::info, sizeof...(Ts)> pack{refl<Ts>()...};
    for (auto i = std::size_t{}; i < pack.size(); ++i) {
      ->fragment struct { typename(%{pack[i]}) unqualid("m", %{i}); };
    }
  }
};

template<typename T>
void f1(T t)
{
  consteval {
    auto info = reflexpr(t);
    auto range = meta::data_member_range(info); // query is not defined for reflected construct
  }
}

int main() {
    constexpr auto t = tuple<int, float, char>{7, 5.5, 'c'};
    f1(t);
}

See https://cppx.godbolt.org/z/1Tq8nE

Thanks,

Roland

TimPhoeniX commented 3 years ago

Hi,

I believe you must use meta::data_member_range on actual type reflection instead of parameter reflection. Reflect T directly or use meta::type_of.

rbock commented 3 years ago

Thanks! That certainly explains it.

Now I wonder: How can I iterate over the data members of an object?

Wouldn't it be nice if meta::data_member_range would allow that?

TimPhoeniX commented 3 years ago

See https://cppx.godbolt.org/z/E7hee1

rbock commented 3 years ago

Nice! Thanks again! Much appreciated :-)