string-view-lite icon indicating copy to clipboard operation
string-view-lite copied to clipboard

Prevent construction/conversion from nullptr

Open mbs-c opened this issue 3 years ago • 1 comments

When introducing string_view into an old codebase, it is quite easy to accidentally leave nonsensical assignments from or comparisons to nullptr in your code. Adding a deleted constructor as follows allows developers to easily identify the offending assignments and comparisons:


#if nssv_CPP11_OR_GREATER
    nssv_constexpr basic_string_view( std::nullptr_t ) nssv_noexcept = delete;
#endif

C++23 will also disallow constructing std::basic_string_view from nullptr: https://github.com/cplusplus/papers/issues/887

mbs-c avatar Feb 04 '22 15:02 mbs-c

Ah, thank you for the pointer Moritz @mbs-c, I wasn't yet aware of this.

I'll add the proposed constructor.

martinmoene avatar Feb 04 '22 16:02 martinmoene