This is the mail archive of the
mailing list for the GCC project.
Re: uninitialized string
On 1/21/2019 6:18 PM, Jonathan Wakely wrote:
On Mon, 21 Jan 2019 at 17:13, Manfred <email@example.com> wrote:
I wonder why int triggers the warning, but std::string does not.
Well for starters, std::string is not the same as int, not even close.
I know, they obviously are very different objects, but that's not the point.
It's pretty obvious when you think about it. Constructing a
std::string calls a constructor, in this case, it calls the
basic_string(const basic_string&) constructor, and passes the *this as
the argument, which makes a copy of the string. The problem is that
the string was never constructed, so it's a copy of garbage.
That's it, the object is used before it is initialized.
Apparently gcc is able to catch this with int (probably other types
too), but not with string.
Failing to warn about it is a longstanding and well-known defect in
GCC, you can find lots of discussion in bugzilla.
The following was reported:
Since this complains about int, but apparently int works today, I was
wondering if anything is going on about string a well.