I don't understand why the first (good) version of the code compiles, but the second doesn't
I have read this, this , this, and of course this but i still do not understand why for one version it compiles, while for the other it doesn't. If somebody could please explain it (like for total dummies), I would really appreciate it.
GOOD version
template <typename As, typename std::enable_if<
std::is_arithmetic<As>::value, As>::type* = nullptr >
As getStringAs(const std::string& arg_name)
{
std::istringstream istr(arg_name);
As val;
istr >> val;
if (istr.fail())
throw std::invalid_argument(arg_name);
return val;
}
BAD version
template <typename As, typename std::enable_if_t<
std::is_arithmetic<As>::value, As> = 0 >
As getStringAs(const std::string& arg_name)
{
std::istringstream istr(arg_name);
As val;
istr >> val;
if (istr.fail())
throw std::invalid_argument(arg_name);
return val;
}
Intended Usage:
int main()
{
return getStringAs<float>("2.f");
}
Thank you very much!
Aucun commentaire:
Enregistrer un commentaire