[PATCH] D24904: Allow StringRef to be constructed with a null pointer

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 26 11:03:50 PDT 2016


zturner added a comment.

In https://reviews.llvm.org/D24904#552650, @clayborg wrote:

> Can we either leave in the:
>
>   StringRef(std::nullptr_t) = delete;
>
>
> Or make the "const char *" constructor explicit? If we don't, we can end up getting default constructed StringRef objects when we don't expect them like in:
>
>   StringRef a("hello");
>   if (a == nullptr) { ... }
>   if (a == NULL) { ... }
>


Leaving the deleted `nullptr_t` constructor seems reasonable.  People shouldn't be checking against `nullptr`, they should be using `.empty()`, and this will allow the compiler to fail in that case.  They can always explicitly invoke the default constructor if they really want it.

Marking the `const char *` constructor explicit means you won't be able to write `foo("Test")` anymore, which would be really unfortunate.  There might be a way to hack around it with some overload / template magic, but I tried and couldn't get it to work.


https://reviews.llvm.org/D24904





More information about the llvm-commits mailing list