std::string add nullptr attribute

Xi Ruoyao xry111@xry111.site
Thu Feb 9 16:30:32 GMT 2023


On Thu, 2023-02-09 at 14:56 +0000, Jonathan Wakely via Gcc-help wrote:
> > Note, my code isn't like this, it is just an example to suggest
> > adding the nullptr attribute, as its clearly already rejected at
> > runtime.
> 
> I assume you mean the nonnull attribute. That was added in 2020 and
> then reverted because it broke some things:

I remember I'd once made the same mistake when I suggested to add
nonnull for ostream::operator<<(const string &) and I was lectured:
nonnull is not only a diagnostic attribute, it also allows the compiler
to assume the parameter is never null and rendering std::string(nullptr)
an undefined behavior.

Then the example may just silently continue to run, instead of throwing
an exception.  It would be an ironic example: an attempt to improve
diagnostic finally made diagnostic more difficult.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University


More information about the Gcc-help mailing list