Questions about std::string::operator=(char)

Jonathan Wakely jwakely.gcc@gmail.com
Sat Mar 27 10:37:06 GMT 2021


On Sat, 27 Mar 2021 at 10:35, Jonathan Wakely wrote:
>
> On Sat, 27 Mar 2021 at 02:26, tika via Libstdc++ <libstdc++@gcc.gnu.org> wrote:
> >
> >
> > Synopsys:?? assigning a char to a std::string makes a string 1 char long,
> > but what happens if that char is zero??? And showing how easily can this
> > UB pass undetected through unit testing...
>
> This is not UB.
>
> >
> > The story:?? I created a stupid bug, that my unit testing did not
> > catch....?? And ended up spending more more time debugging the test than
> > fixing my bug.
> >
> > The bug itself is kind of boring...?? Using the return value of int
> > snd_get_name(int, char*) C function from the alsa library, and putting
> > an int into a string. That's what I think could be a rather common mistake.

See the proposal
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2037r1.html

The C++ committee rejected the proposal, so it won't be part of the
C++ standard, but it might help you understand the behaviour you see
(and why GCC is correct).


More information about the Libstdc++ mailing list