std::cout breaks after nullptr

Xi Ruoyao xry111@mengyan1223.wang
Mon Mar 25 11:58:00 GMT 2019


On 2019-03-24 19:40 +0000, Jonny Grant wrote:
> Can I ask, for this nonnull attribute, would it also detect for 
> std::cin, std::err, std::clog as well as std::cout ? Worth adding for 
> std::wcin, std::wcout, std::cerr, std::wclog too.

Adding this attribute to basic_ostream will cover std::cout, std::clog,
std::cerr etc.  For input streams we can't do that because:

char *useless = nullptr;
cin >> useless;  // expect EOF here

is perfectly valid when cin reaches EOF (for e.g. though this code seems
stupid).

> Are there other places where nullptr would not be valid? There are lots 
> of classes, eg <streambuf>

Adding this for basic_ostream also applies to ostringstream because it inherits
basic_ostream.

Unfortunately Marc just told me nonnull is also an optimization attribute.  So
adding it would optimize the null check in operator<< away.  Maybe we need a new
attribute?  Called "warn_if_null"?
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University



More information about the Gcc-help mailing list