0

So, unfortunately, I cannot reproduce the error, since it seems to be fixed in newer g++ versions, but I cannot compile the following code:

#include <charconv>
#include <concepts>
#include <optional>
#include <string_view>

template<std::floating_point T>
std::optional<T> to_float(std::string_view s)
{
    T value;
    if (std::from_chars(s.data(), s.data() + s.size(), value).ec == std::errc{})
        return value;
    return {};
}
  
int main()
{
    to_float<float>("0");
    return 0;
}

It yields the following error:

In instantiation of ‘std::optional<_Tp> to_float(std::string_view) [with T = float; std::string_view = std::basic_string_view]’: main.cpp:633:92: required from here main.cpp:36:24: error: no matching function for call to ‘from_chars(std::basic_string_view::const_pointer, std::basic_string_view::const_pointer, float&)’ 36 | if (std::from_chars(s.data(), s.data() + s.size(), value).ec == std::errc{}) | ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from main.cpp:25: /usr/include/c++/10/charconv:595:5: note: candidate: ‘template 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/include/c++/10/charconv:595:5: note: template argument deduction/substitution failed: In file included from /usr/include/c++/10/bits/move.h:57, from /usr/include/c++/10/bits/stl_pair.h:59, from /usr/include/c++/10/bits/stl_algobase.h:64, from /usr/include/c++/10/vector:60: /usr/include/c++/10/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/include/c++/10/charconv:584:11: required by substitution of ‘template using __integer_from_chars_result_type = std::enable_if_t<std::_or<std::_or<std::is_same<typename std::remove_cv< >::type, signed char>, std::is_same<typename std::remove_cv<

::type, short int>, std::is_same<typename std::remove_cv< >::type, int>, std::is_same<typename std::remove_cv< >::type, long int>, std::is_same<typename std::remove_cv< ::type, long long int> >, std::_or<std::is_same<typename std::remove_cv< >::type, unsigned char>, std::is_same<typename std::remove_cv< ::type, short unsigned int>, std::is_same<typename std::remove_cv< >::type, unsigned int>, std::is_same<typename std::remove_cv< >::type, long unsigned int>, std::is_same<typename std::remove_cv< ::type, long long unsigned int> >, std::is_same<char, typename std::remove_cv< >::type> >::value, std::from_chars_result> [with _Tp = float]’ /usr/include/c++/10/charconv:595:5: required by substitution of ‘template std::__detail::__integer_from_chars_result_type<_Tp> std::from_chars(const char*, const char*, _Tp&, int) [with _Tp = float]’ main.cpp:36:24: required from ‘std::optional<_Tp> to_float(std::string_view) [with T = float; std::string_view = std::basic_string_view]’ main.cpp:633:92: required from here /usr/include/c++/10/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;

I'm using g++ version is g++ (Debian 10.2.1-6) 10.2.1 20210110. Any ideas how I can solve this issue? Using a newer g++ version is not possible, unfortunately.

1 Answer 1

1

The floating point overloads of std::from_chars are implemented in libstdc++ only starting with version 11.

See https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html. Look for "P0067R5".

So the functionality you try to use is simply not present in that GCC version.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.