I want to split a string by a token with std::views::split
, and for each retrieved substring invoke the std::from_chars
function.
Here is an MRE (https://godbolt.org/z/1K71qo9s4) that compiles successfully on GCC but fails to compile on MSVC:
#include <iostream>
#include <ranges>
#include <string>
#include <charconv>
int main()
{
std::string source{"10%15%20%35"};
for ( auto i : source | std::views::split('%') )
{
int x;
std::from_chars( i.begin().base(), i.end().base(), x );
std::cout << x << ' ';
}
}
What strange is that according to cppreference, the behavior of std::views::split
was changed in P2210R2, providing base()
function which effect is
Equivalent to
return cur_
Then, according to compiler support page, the P2210R2
is supported in MSVC since 19.31, so the example should work.
If this were working how you think it does,
i.begin().base()
should be astd::string::iterator
, which has to beconst char*
(or at least convertible to aconst char*
) to pass tostd::from_chars
.Looks like libstdc++ also doesn't implement this properly either, and
i.begin()
is returning astd::string::iterator
instead of astd::ranges::views::split_view<std::string>::iterator
. It just so happens that libstdc++'sstd::string::iterator
has a function calledbase()
that returns a pointer.You have to get a
const char*
pointer anyways, which you can do withstd::to_address
:Or something like: