Question about P0590R0: Reflection on members and reference members

104 views
Skip to first unread message

Guillaume Racicot

unread,
Jul 18, 2017, 2:58:00 PM7/18/17
to SG 7 - Reflection

Hi there,


I've been reflecting (pun intended) about the P0590R0 proposal. There is a part that I find missing: You cannot iterate on member references. In the document, the concept MemberVariable has the 

T C::* pointer() defined. However, this is not possible with reference members.


The solution I think about is this: when accessing members through $T.member_variables(), the member meta::member_variable<M> should have something like .reference(T) or .data(T), where T is an object that has that member reflected. This would allow this syntax instead:


template<HashAlgorithm H, SimpleStruct T>
   void hash_append(H& h, T const& s) {
     for (MemberVariable v : $T.member_variables())
       hash_append(h, v.data(s));

}


 

in this example, v.data(s) return type would be deduced as auto&&, as it would always return a reference to the member, even if that member is int, or if s is const, just as this example.


A (very) rough approximation of how the definition of the .data(s) function would look like this:


struct member_variable {

   
// ...

   
template<typename U> // sfinae-ing out when U is the wrong type

   
auto&& data(U&& instance) const {

       
return std::forward<U>(instance). /* member name */;

   
}

   
// ...

};



For completeness, bool is_reference() should be added to the MemberVariable concept, so we can differentiate between member reference and other members.


If there's a member reference, this loop will allow to iterate on it, as before it was not possible because it forced member pointer syntax.


Of course another solution would be to allow pointer to member reference, but I don't think it would be a great idea.


What do you think? Should we defined another way of iterating on members when there's reference member involved? Should we be able to reflect on reference member at all? Thanks for your response. I'm very open to feedback about this. In my opinion, reference member, just as any other member, should be subject to reflection.


Thanks everyone for your response.

Reply all
Reply to author
Forward
0 new messages