-
Notifications
You must be signed in to change notification settings - Fork 452
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
Support no default value enum_switch #199
Comments
If you need to iterate over all values then use
|
Yes, sorry I made a mistake in the example above. return magic_enum::enum_switch( overloaded{
[]( my_enum::val1 arg ) -> int { /* handle val1 enum */ return 1; },
[]( auto arg ) -> int { /* handle the rest */ throw notimplemented; /* or just return sth */ }
}, enumValue); I'm taking about return value of enum_switch template <typename Result = void, typename E, typename Lambda>
constexpr Result enum_switch(Lambda&& lambda, E value);
template <typename Result, typename E, typename Lambda>
constexpr Result enum_switch(Lambda&& lambda, E value, Result&& result); If I want to return a value from enum_switch (Result is not void) I need to pass additional argument and It'd be nice not being forced to do so. |
return magic_enum::enum_switch<int>( overloaded{
[]( my_enum::val1 arg ) -> int { /* handle val1 enum */ return 1; },
[]( auto arg ) -> int { /* handle the rest */ throw notimplemented; /* or just return sth */ }
}, enumValue); see https://github.com/Neargye/magic_enum/blob/master/example/example_switch.cpp |
ok, but I still need to specify the return type explicitly. std::visit( overloaded { []( const Type1 val ) { return int(1); },
[]( const Type2 val ) { return float(2); }
}); // compilation error
std::visit( overloaded { []( const Type1 val ) { return int(1); },
[]( const Type2 val ) -> int { return float(2); }
}); // fine
std::visit( overloaded { []( const Type1 val ) { return int(1); },
[]( const Type2 val ) { return int(2); }
}); // fine |
I'll see what i can do |
The switch is not required to handle all cases (especially unrecognized values), so when explicitly isn't set the result (or the type) for the What will be the result for example 4 different result type without explicitly set the result? What if someone didn't handle all case, and lambda can't be instantiated with some value? Many unanswered question will come up if we enable enum_switch result type deduction from only the callback. So the current rule is: and there are no "better" way to do otherwise. |
@schaumb If you don't mind, I can add the necessary traits for the checks inside the function. |
If I wrote a swich, I don't need to write all case / default. I think this is an another use-case, and probably not a
|
Yes, this is what I meant, not having to specify the return type. |
I'll take it to the master and release after some additional testing. |
Fix in master |
would it be possible to support enum_switch the way that we're used to use overload pattern with std::visit?
e.g
instead of
I think forcing adding default Result produces boilerplate.
The text was updated successfully, but these errors were encountered: