gcc shows error for std::from_chars( … double & ) [duplicate]

  c++, c++17, gcc

In the simple program:

#include <charconv>

int main()
{
    const char * str = "42";

    int iresult;
    std::from_chars(str, str+2, iresult);

    double result;
    std::from_chars(str, str+2, result);
}

GCC compiler accepts the first part with int-argument, but shows an error with double-argument:

main.cpp: In function 'int main()':
main.cpp:11:39: error: no matching function for call to 'from_chars(const char*&, const char*, double&)'
   11 |     std::from_chars(str, str+2, result);
      |                                       ^
In file included from main.cpp:1:
/usr/local/include/c++/10.2.0/charconv:595:5: note: candidate: 'template<class _Tp> std::__detail::__integer_from_chars_result_type<_Tp> std::from_chars(const char*, const char*, _Tp&, int)'
  595 |     from_chars(const char* __first, const char* __last, _Tp& __value,
      |     ^~~~~~~~~~
/usr/local/include/c++/10.2.0/charconv:595:5: note:   template argument deduction/substitution failed:
In file included from /usr/local/include/c++/10.2.0/charconv:40,
                 from main.cpp:1:
/usr/local/include/c++/10.2.0/type_traits: In substitution of 'template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = std::from_chars_result]':
/usr/local/include/c++/10.2.0/charconv:584:11:   required by substitution of 'template<class _Tp> using __integer_from_chars_result_type = std::enable_if_t<std::__or_<std::__or_<std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, signed char>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, short int>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, int>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, long int>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, long long int> >, std::__or_<std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, unsigned char>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, short unsigned int>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, unsigned int>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, long unsigned int>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, long long unsigned int> >, std::is_same<char, typename std::remove_cv< <template-parameter-1-1> >::type> >::value, std::from_chars_result> [with _Tp = double]'
/usr/local/include/c++/10.2.0/charconv:595:5:   required by substitution of 'template<class _Tp> std::__detail::__integer_from_chars_result_type<_Tp> std::from_chars(const char*, const char*, _Tp&, int) [with _Tp = double]'
main.cpp:11:39:   required from here
/usr/local/include/c++/10.2.0/type_traits:2554:11: error: no type named 'type' in 'struct std::enable_if<false, std::from_chars_result>'
 2554 |     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
      |           ^~~~~~~~~~~

How can I use std::from_chars to read a double value?

Source: Windows Questions C++

One Reply to “gcc shows error for std::from_chars( … double & ) [duplicate]”

LEAVE A COMMENT