-
Notifications
You must be signed in to change notification settings - Fork 9
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
Conversation
Codecov Report
@@ 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
|
tests/unit/utils/test_safe_union.cc
Outdated
x.init<int>(); | ||
x.get<int>() = 10; | ||
|
||
EXPECT_EQ(x.template switchOn<TestFunctor>(x), 2); |
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.
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?
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. |
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? |
Generic lambdas is in C++14 |
Or, the caller can actually define a functor, just like you did in the test, and construct it with suitable arguments |
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 |
71463c5
to
f277da5
Compare
Along those lines, the functor's |
Fixes #922
Another spin off of #882, that provides a lot more safety when switching.