fix node::value_or() to act like node::value() (#50)

auto table = toml::table{{{"value", 10}}};
const toml::node *value = table.get("value");
if(value && value->is_number())
{
    double number = value->value_or<double>(0);
    cout << number << endl;
}
The original output was 0. This change will output 10.0.
The old way would see that it was an integer (a native type),
then use the integer conversion, but since the type being requested
was a double, it would return the default value.
This commit is contained in:
whiterabbit963 2020-07-30 13:41:28 -04:00 committed by GitHub
parent a1527d5529
commit 52736559d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 28 deletions

View File

@ -190,6 +190,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
- **[@shdnx](https://github.com/shdnx)** - Fixed a bug on GCC 8.2.0 and some meson config issues
- **[@traversaro](https://github.com/traversaro)** - Added vcpkg support and reported a bunch of bugs
- **[@ximion](https://github.com/ximion)** - Added support for installation with meson
- **[@whiterabbit963](https://github.com/whiterabbit963)** - Fixed a bug with value_or conversions
<br>

View File

@ -800,21 +800,12 @@ TOML_NAMESPACE_START
// prevent additional compiler error spam when the static_assert fails by gating behind if constexpr
if constexpr (traits::is_native || traits::can_represent_native || traits::can_partially_represent_native)
{
if constexpr (traits::is_native)
{
if (type() == node_type_of<value_type>)
return *ref_cast<typename traits::native_type>();
return std::forward<T>(default_value);
}
if (auto val = this->value<value_type>())
return *val;
if constexpr (std::is_pointer_v<value_type>)
return value_type{ default_value };
else
{
if (auto val = this->value<value_type>())
return *val;
if constexpr (std::is_pointer_v<value_type>)
return value_type{ default_value };
else
return std::forward<T>(default_value);
}
return std::forward<T>(default_value);
}
}
}

View File

@ -3148,21 +3148,12 @@ TOML_NAMESPACE_START
// prevent additional compiler error spam when the static_assert fails by gating behind if constexpr
if constexpr (traits::is_native || traits::can_represent_native || traits::can_partially_represent_native)
{
if constexpr (traits::is_native)
{
if (type() == node_type_of<value_type>)
return *ref_cast<typename traits::native_type>();
return std::forward<T>(default_value);
}
if (auto val = this->value<value_type>())
return *val;
if constexpr (std::is_pointer_v<value_type>)
return value_type{ default_value };
else
{
if (auto val = this->value<value_type>())
return *val;
if constexpr (std::is_pointer_v<value_type>)
return value_type{ default_value };
else
return std::forward<T>(default_value);
}
return std::forward<T>(default_value);
}
}
}