New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
variant: visit lambda and value-initialize by index #9172
base: master
Are you sure you want to change the base?
variant: visit lambda and value-initialize by index #9172
Conversation
b86dcd1
to
6c6245e
Compare
6c6245e
to
62fdacc
Compare
{ | ||
return boost::apply_visitor(std::forward<VisitorT>(visitor), m_value); | ||
} | ||
|
||
/// value initialize the variant based on a type index | ||
void value_initialize_to_type_index(const int which) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what situations would you need to construct based on index rather than type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deserialization
Edit: more specifically for deserialization of variants which have complex logic differing between the types. Instead of doing the logic at a high level, then assigning the variant, you can break down the process into independent chunks. When you de-serialize a selector, you can apply the selector (in this case value initializing the variant), then fire away and let the specific code for that variant handle the logic. In my opinion, this leads to much cleaner variant de-serialization code when the variant is non-trivial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something similar is already done in the current variant de serialization code:
monero/src/serialization/variant.h
Lines 74 to 80 in 059028a
current_type x; | |
if(!do_serialize(ar, x)) | |
{ | |
ar.set_fail(); | |
return false; | |
} | |
v = x; |
Two new features for
tools::variant
: