C++ Logo

std-proposals

Advanced search

Re: [std-proposals] operator*&

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Sat, 19 Oct 2024 18:24:18 +0100
On Sat, 19 Oct 2024, 14:20 Giuseppe D'Angelo via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> Il 19/10/24 15:07, Frederick Virchanza Gotham via Std-Proposals ha scritto:
> > I've had the following in my codebase for a few years:
> >
> > string_view first = SomeFunction1();
> > uint16_t a;
> > std::from_chars( first.begin(), first.end(), a );
> >
> > This worked fine until I compiled with the Microsoft compiler, because
> > on the Microsoft compiler, the string_view::iterator is not a simple
> > typedef for a char pointer. I could have just changed it to:
> >
> > std::from_chars( &*first.begin(), &*first.end(), a);
> >
> > but this is undefined behaviour and it sets off the debugger in the
> > GNU compiler. So instead I changed it to:
> >
> > uint16_t a = 0u;
> > if ( first.size() ) std::from_chars( &first.front(),
> > &first.back() + 1, a );
> >
> > Which got me thinking . . . maybe it would be handy if we could write
> > an iterator as follows:
>
> Is this now turning into StackOverflow?
>
> std::string_view::iterator models std::contiguous_iterator and therefore
> you already have a way, it's called std::to_address.
>

Indeed. But we badly need more user-friendly interfaces to from_chars.

We should just add std::stoi(std::string_view). We didn't add it originally
because it was typically implemented with strtol which needs a
null-terminated string, but I'm pretty sure we can make std::stoi use
from_chars now, without changing the behaviour. And that doesn't need a
null-terminated string, so can work with both std::string and
std::string_view.

std::stoi(wstring_view) is trickier, as we don't have a wide from_chars, so
that still needs to use wcstol, which requires a null-terminated string.

Received on 2024-10-19 17:25:37