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

922 Implement static type switch on safe union #923

Merged
merged 5 commits into from
Jul 15, 2020

Conversation

lifflander
Copy link
Collaborator

Fixes #922

Another spin off of #882, that provides a lot more safety when switching.

@codecov
Copy link

codecov bot commented Jul 13, 2020

Codecov Report

Merging #923 into develop will increase coverage by 0.03%.
The diff coverage is 90.47%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #923      +/-   ##
===========================================
+ Coverage    82.76%   82.79%   +0.03%     
===========================================
  Files          356      356              
  Lines        11188    11232      +44     
===========================================
+ Hits          9260     9300      +40     
- Misses        1928     1932       +4     
Impacted Files Coverage Δ
src/vt/utils/adt/union.h 84.21% <90.00%> (+0.63%) ⬆️
tests/unit/utils/test_safe_union.cc 96.92% <90.62%> (-2.06%) ⬇️

x.init<int>();
x.get<int>() = 10;

EXPECT_EQ(x.template switchOn<TestFunctor>(x), 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really funny looking interface. If it's a member function of x, why should the caller have to pass x again? Shouldn't the functor be taking exactly an argument of the selected type?

@PhilMiller
Copy link
Member

I'm fine with approving this as is, if it's really what you want, but it seems like a very strange way to express it.

@lifflander
Copy link
Collaborator Author

lifflander commented Jul 14, 2020

I'm fine with approving this as is, if it's really what you want, but it seems like a very strange way to express it.

@PhilMiller I've designed it so you can pass whatever arguments you want to the type-switched functor. In the actual use case, I have several args to pass. Unless I always pass as the first argument, I don't know how to easily "automatically" pass the union, if that's what is needed while keeping the arguments aligned in caller/callee.

If you have a better idea, I'm completely open to it.

@PhilMiller
Copy link
Member

I think my instinct is to let the functor capture any other arguments it may need, and to pass the active value from the union as the only argument. That eliminates the variadic templating and the argument forwarding entirely.

@lifflander
Copy link
Collaborator Author

I think my instinct is to let the functor capture any other arguments it may need, and to pass the active value from the union as the only argument. That eliminates the variadic templating and the argument forwarding entirely.

The functor can't be a lambda in C++14 since templated lambdas don't exist. Thus, there's no way to "capture". Am I misunderstanding what you are suggesting?

@PhilMiller
Copy link
Member

Generic lambdas is in C++14

@PhilMiller
Copy link
Member

Or, the caller can actually define a functor, just like you did in the test, and construct it with suitable arguments

@lifflander
Copy link
Collaborator Author

lifflander commented Jul 15, 2020

I'm still missing how this would work. The functor is templated:

template <typename T>
struct MyFunctor {
  void operator()() const { }
};

The switch instantiates the functor with the active type. The user can't create the functor, because they don't know what type the union is on, and thus wouldn't be able to construct it with that type T. Are you suggesting that we move the template inside to the operator() and template it over T?

@lifflander lifflander force-pushed the 922-static-switch-union branch from 71463c5 to f277da5 Compare July 15, 2020 03:48
@PhilMiller
Copy link
Member

Along those lines, the functor's operator() can either be a template, or an overload set. The union's switch would just need to call it with the appropriate argument type, and the compiler will do the right thing.

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

Successfully merging this pull request may close these issues.

Implement static switch based on type for safe union
2 participants