Ensure `chars_format` is defined for older versions of libstdc++

Versions older than 10 don't even define this enum class. Note that libc++
always seems to define it even though it doesn't implement the FP overloads
at all at this point.

By the way, this means the minimum GCC version is 8 and the minimum libc++
version is 7 because for this code to work the `charconv` header still
needs to be present at all.
This commit is contained in:
Martchus 2023-12-11 19:58:35 +01:00
parent 1264a7e9c5
commit d8605b50b0
1 changed files with 8 additions and 2 deletions

View File

@ -17,8 +17,14 @@ using namespace std;
namespace CppUtilities {
/// \cond
inline std::from_chars_result from_chars(
const char *first, const char *last, double &value, std::chars_format fmt = std::chars_format::general) noexcept
#if defined(__GLIBCXX__) && _GLIBCXX_RELEASE < 10
enum class chars_format { scientific = 1, fixed = 2, hex = 4, general = fixed | scientific };
#else
using char_format = std::chars_format;
#endif
inline std::from_chars_result from_chars(const char *first, const char *last, double &value, chars_format fmt = chars_format::general) noexcept
{
#if defined(_LIBCPP_VERSION) || (defined(__GLIBCXX__) && _GLIBCXX_RELEASE < 11)
// workaround std::from_chars() not being implemented for floating point numbers in libc++ and older libstdc++ versions