Reputation: 53
I have a little function that uses std::from_chars
to create a date object from a std::string_view
, while it compiles using clang or gcc under ubuntu it doesn't using msvc under windows.
date::year_month_day parseDate(const std::string_view& s)
{
int y { 0 }, m { 0 }, d { 0 };
std::from_chars(s.begin(), s.begin() + 4, y);
std::from_chars(s.begin() + 5, s.begin() + 7, m);
std::from_chars(s.begin() + 8, s.begin() + 10, d);
return date::year { y } / m / d;
}
For each of std::from_chars
call the following error is displayed at compile time:
'std::from_chars': none of the 14 overloads could convert all the argument types
The compiler then proceeds to make a list of possible overloads and i clearly see the one that I'm trying to use:
'std::from_chars_result std::from_chars(const char *const ,const char *const ,int &,const int) noexcept'
The last parameter has a default value. Can someone explain me what I'm doing wrong?
Upvotes: 2
Views: 1190
Reputation: 1196
The other answer is correct - from_chars
doesn't play nicely with string_view
.
However, for others finding the same issue in future, there is a proposal to add the relevant overloads to from_chars
.
Progress on this can be tracked here.
Upvotes: 3
Reputation: 53
As mentioned by Some programmer dude and Ruks, std::string_view.begin()
doesn't return a const char *const
, std::string_view.data()
was the method i was looking for.
Upvotes: 2