std::string add nullptr attribute

Jonathan Wakely jwakely.gcc@gmail.com
Thu Feb 9 14:56:12 GMT 2023


On Thu, 9 Feb 2023 at 13:28, Jonny Grant wrote:
>
> Hello
> Could GCC's STL implementation add nullptr attribute to prevent code like this compiling?
>
> g++ -Wall -o string string.cpp
>
>  std::string c(nullptr);
>
>
>  At least GCC STL does reject it at runtime:
>
> terminate called after throwing an instance of 'std::logic_error'
>   what():  basic_string: construction from null is not valid
> Aborted (core dumped)
>
>
> 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:

commit 2635f9e5086318f4560997d9741fdda496b9c801
Author: Ville Voutilainen
Date:   Mon Jun 29 23:59:34 2020

   Revert "Add a __nonnnull__ attribute to std::string's _CharT* constructor"

   This reverts commit b26fd416fb0a734d3f3e56629b6dff2e3c25dd40.

commit b26fd416fb0a734d3f3e56629b6dff2e3c25dd40
Author: Ville Voutilainen
Date:   Sun Jun 28 22:47:05 2020

   Add a __nonnnull__ attribute to std::string's _CharT* constructor

           Add a __nonnnull__ attribute to std::string's _CharT* constructor
           * include/bits/basic_string.h (string(_CharT*, const _Alloc&)):
           Add a __nonnull__ attribute.
           * testsuite/21_strings/basic_string/cons/char/nonnull.cc: New.
           * testsuite/21_strings/basic_string/cons/wchar_t/nonnull.cc:
Likewise.


I don't know if anybody analyzed why it broke tests, to see if it
could be made to work.


More information about the Gcc-help mailing list