_from_string_nothrow return specified default value
sena73 opened this issue · comments
Hi, I try to create a function which will return specified default value if string can not be converted.
Here is my code:
#include <enum.h>
BETTER_ENUM(TestEnum, int, Test1, Test2, Test3)
template<class EnumClass>
EnumClass StrToEnum(const std::string &str, EnumClass defval)
{
auto maybe_val = EnumClass::_from_string_nothrow(str.c_str());
return (maybe_val) ? *maybe_val : defval;
}
When I try to use this, it works only if I specify type of the template parameter:
auto val = StrToEnum<TestEnum>("somestring", TestEnum::Test1);
The following wouldn't compile:
auto val = StrToEnum("somestring", TestEnum::Test1);
The error is :
‘_from_string_nothrow’ is not a member of ‘TestEnum::_enumerated’
auto maybe_val = EnumClass::_from_string_nothrow(str.c_str());
I tried to put _enumerated into the function parameter type:
template<class EnumClass>
EnumClass StrToEnum(const std::string &str, typename EnumClass::_enumerated defval)
But then I get an error:
no matching function for call to ‘StrToEnum(std::__cxx11::string&, TestEnum::_enumerated)’
...
template argument deduction/substitution failed:
...
note: couldn't deduce template parameter ‘EnumClass’
Can you please advise something?
I propose to add a variant of _from_string_nothrow() which will take default parameter to return
Enum Enum::_from_string_nothrow(char *str, Enum default);
Hi @sena73, it seems the original notification about this issue went to my spam folder :/ What did you end up doing for this?