Explicit conversion of SmallString to std::string

Yaron Keren yaron.keren at gmail.com
Wed Oct 15 08:40:59 PDT 2014


SmallString has implicit conversion to StringRef:

 operator StringRef() const { return str(); }

Explicit .str() helps when you want to use one of the StringRef useful
member functions without using an explicit StringRef temporary, such as:

  SmallString<128> Name;
  if (Name.str().startswith("blah"))

StringRef does not have a constructor from SmallString probably to avoid
circular dependency.

BTW, .str() is already inconsistent since SmallString.str() is StringRef
but StringRef.str() is std::string.

As for name, would SmallString.to_string() be better?


2014-10-15 18:12 GMT+03:00 David Blaikie <dblaikie at gmail.com>:

> using the same identifier as a standard type like 'string' is probably a
> bit sketchy... though I'm not sure what other names might suit
>
> Why does SmallString need an explicit StringRef-izing function anyway
> (thus freeing up the name 'str')? That seems like a reasonable implicit
> conversion that'd rarely be needed to be explicit... but perhaps I'm wrong.


On Wed, Oct 15, 2014 at 2:53 AM, Yaron Keren <yaron.keren at gmail.com> wrote:

> SmallString conversion to std::string is currently done by going through a
> StringRef, S.str().str(). This patch provides a direct conversion function,
> S.string().
>
> Yaron
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141015/f9639e84/attachment.html>


More information about the llvm-commits mailing list