bloomberg / clang-p2996

Experimental clang support for WG21 P2996 (Reflection).
https://github.com/bloomberg/clang-p2996/tree/p2996/P2996.md
60 stars 12 forks source link

Retrieve std::meta::info of an std::meta::info? #97

Closed krzikalla closed 4 weeks ago

krzikalla commented 1 month ago

I wonder whether this is the right place to ask questions about your reflections approach. Sorry, if this is the wrong place. Anyway... I am trying to create a struct, which mirrors the non-static data members of another struct. Those reflected data member have special types. Such a type should allow me to access the original data member, when it gets an object to the original struct. You find my experiments here:

https://godbolt.org/z/bEvr1eW9x

My code always fails in substitute. IMHO the most promising approach is

template<class T, std::meta::info MemberInfo>
struct value_expression
{
};
//...
  for (std::meta::info member : old_members) {
    auto mem_type = substitute(^value_expression, {type, member});
    //...
  }

But substitute "dereflects" member expression. Writing ^member in the substitute call doesn't help: error: non-type template argument is not a constant expression. Do I have a chance to reach my goal with the current reflection implementation?

Yaraslaut commented 1 month ago

Hi, Here is a minimal example of copying data members https://godbolt.org/z/17rWE5MxG

krzikalla commented 1 month ago

Thanks for the quick reply. Thus the trick is to use expand and a templatized create_type function instead of the iterative approach shown in example §3.10 (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2996r5.html#struct-to-struct-of-arrays). Now it works:

https://godbolt.org/z/zvbPn3b5e

Yaraslaut commented 1 month ago

Great that you managed to fix your code :) I am still puzzled about what the code does exactly though xD

krzikalla commented 1 month ago

It mimics the data member access operator (operator.). Eventually the expression data[i].x will return a std::simd value. For more details you might refer to my cppcon24 talk (github doesn't let me send private messages, but you will find it, my name is rather specific).

Yaraslaut commented 1 month ago

Oh, so this is a piece of puzzle i was missing to see a reason to have a layer of misdirection in data access, will definitely have a look at your talk once it will be online :) Will be happy to look at the final result, as well. Also, are you considering different memory layouts for aligned loading inside simd?

katzdm commented 1 month ago

@krzikalla Sounds like you figured out how to achieve what you're trying to achieve - Is there still an issue, or should we close this one?

krzikalla commented 4 weeks ago

Can be closed, thank you.