How to use union metafunction to replace variant member and member functions that call std::visit. #766
Replies: 3 comments 18 replies
-
Maybe the more experienced people here can explain better but I think pattern matching is the right answer to this use case. |
Beta Was this translation helpful? Give feedback.
-
This looks like something you would build on top of |
Beta Was this translation helpful? Give feedback.
-
This is regarding the original problem posted by @gregmarr, maybe the
Specifics about this can be discusssed. |
Beta Was this translation helpful? Give feedback.
-
This came up in a C++ study group meeting today about ways to replace virtual functions in a class hierarchy that has a fixed set of member classes.
Here is a solution from today using
std::variant
andstd::visit
.One of the discussion points was that it would be nice to be able to get rid of the
std::visit
boilerplate and be able to automatically generate a "call this function on the active member of the variant" member function.Thinking about how we could do this in Cpp2 with the union metafunction, I was able to get this far:
This is even worse in terms of having to do the boilerplate function, as you have to individually name the function. You can't use
std::visit
because you don't have avariant
, unless we make the union metafunction have all the hooks that visit uses from variant, which isn't out of the realm of possibility. That would still require us to write thestd::visit
boilerplate.Looking at the syntax in P0707 Section 4.2.2
property metaclass function
, I think we could do something like this:and the metafunction handler would rewrite the body of the visitor function like the above, similar to how it generates the destructor now.
Beta Was this translation helpful? Give feedback.
All reactions