[cfe-dev] What is the StringRef equivalent of NULL?

Bo Persson bop at gmb.dk
Wed Aug 18 04:38:27 PDT 2010




On 18 aug 2010 10:39 "Peter Davies" <ultratwo at gmail.com> wrote:
> >If someone believes his code can contain null pointers, the check
> >could be before calling the constructor.
> The only exception is a copy constructor.
> Consider this:
> void foo(const char * bar) { ... }
> 
> I can convert it to this:
> void foo(llvm::StringRef bar) { /* converted as needed */ ... }
> 
> Unless bar can be NULL, in which case I have to add set check too all
> the callers (ugh!). To make matters worse, if I miss one, the program
> still type-checks.
> 
>

You can get your checks with an extra overload

void foo(const char* bar)
{ bar == 0 ? foo(llvm::StringRef()) : foo(llvm::Stringref(bar)); }

That way the cost is payed when possibly needed, and not by everyone creating a StringRef.


Bo Persson




More information about the cfe-dev mailing list