Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeffro256
Copy link
Contributor

Two new features for tools::variant:

  • Visit by lambda instead of static visitor (possible now because result type is deduced)
  • Value-initialize the variant based on a type index

@jeffro256 jeffro256 force-pushed the variant_vi_index_and_lambda_m branch 3 times, most recently from b86dcd1 to 6c6245e Compare February 14, 2024 01:49
{
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)
Copy link
Contributor

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?

Copy link
Contributor Author

@jeffro256 jeffro256 Feb 15, 2024

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.

Copy link
Contributor Author

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:

current_type x;
if(!do_serialize(ar, x))
{
ar.set_fail();
return false;
}
v = x;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants