Making it official with a CL.
See http://lists.llvm.org/pipermail/llvm-dev/2016-September/105163.html for context and discussion.
Will wait a little while to commit this regardless of LGTM just in case someone has an objection.
Differential D24904
Allow StringRef to be constructed with a null pointer zturner on Sep 25 2016, 10:00 PM. Authored by
Details Making it official with a CL. See http://lists.llvm.org/pipermail/llvm-dev/2016-September/105163.html for context and discussion. Will wait a little while to commit this regardless of LGTM just in case someone has an objection.
Diff Detail
Event TimelineComment Actions The 0 is for the length, not the actual pointer. The pointer is just initialized with whatever you give to the constructor. Comment Actions 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) { ... } Comment Actions
Oh right, read it too quickly. I agree with Greg that keeping the deleted nullptr_t ctor is also sensible for this pass. It would be great to evaluate what happens when the const char* ctor is made explicit. Comment Actions 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. Comment Actions
I have a patch that does that (adding the template ctor for string literal), and I'm updating the LLVM codebase locally before submitting. Comment Actions It we have the deleted nullptr constructor, it actually takes care of the "== NULL" case as well because it will complain it doesn't know which function should be used, so things still fails during compilation which is good. Comment Actions Mind if I take a quick look before you submit? I tried my darndest and couldn't get it to work, so idk if you're just better than me or if it doesn't work with MSVC. :) Either way I want to try it locally and see if it works. Comment Actions You should be able to make string literals work if you use a template that takes an array of char with the size as a template parameter. Comment Actions I thought so too, but in my tests given: template<unsigned N> void foo(const char (&S)[N]) { } void foo(const char *S) { } I could never get the compiler to invoke the first one, no matter what I passed in. It's possible I was missing some subtlety though, I'll just try it with Mehdi's forthcoming patch and see what I get. Comment Actions This revision LGTM, making the "const char*" ctor explicit can & should be done as a second step, thanks! |