Is printing a null-pointer Undefined Behavior?

后端 未结 2 933
旧巷少年郎
旧巷少年郎 2021-01-06 00:44

When studying the sample code for this question I had assumed it was Undefined Behaviour which was preventing subsequent uses of std::cout from printing. But it

2条回答
  •  不思量自难忘°
    2021-01-06 01:30

    gcc ostream.tcc line 319:

    template
      basic_ostream<_CharT, _Traits>&
      operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
      {
        if (!__s)
    __out.setstate(ios_base::badbit);
    

    gcc is simply performing a check that the standard does not guarantee, which is fine because it's undefined anyway.

提交回复
热议问题