I'm looking at the documentation on Twine at http://llvm.org/docs/ProgrammersManual.html#llvm-adt-twine-h and it gives example code: void foo(const Twine &T); ... StringRef X = ... unsigned i = ... foo(X + "." + Twine(i)); How exactly does that last line work? Since addition is left associative, I would expect it to be parsed as (X + ".") ... so it's trying to add a StringRef and a const char* before Twine can come into the picture at all? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190429/6d3f6c7c/attachment.html>
I believe StringRef + const char* is overloaded at the bottom of Twine.h. And without those overloads it might try to implicitly convert both StringRef and const char* to Twine and then add them since StringRef + const char * wouldn't otherwise be defined. inline Twine operator+(const Twine &LHS, const Twine &RHS) { return LHS.concat(RHS); } /// Additional overload to guarantee simplified codegen; this is equivalent to /// concat(). inline Twine operator+(const char *LHS, const StringRef &RHS) { return Twine(LHS, RHS); } /// Additional overload to guarantee simplified codegen; this is equivalent to /// concat(). inline Twine operator+(const StringRef &LHS, const char *RHS) { return Twine(LHS, RHS); } ~Craig On Mon, Apr 29, 2019 at 3:18 PM Russell Wallace via llvm-dev < llvm-dev at lists.llvm.org> wrote:> I'm looking at the documentation on Twine at > http://llvm.org/docs/ProgrammersManual.html#llvm-adt-twine-h and it gives > example code: > > void foo(const Twine &T); > ... > StringRef X = ... > unsigned i = ... > foo(X + "." + Twine(i)); > > How exactly does that last line work? Since addition is left associative, > I would expect it to be parsed as (X + ".") ... so it's trying to add a > StringRef and a const char* before Twine can come into the picture at all? > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190429/62109fca/attachment.html>
Ah, that makes sense. Thanks! On Mon, Apr 29, 2019 at 11:43 PM Craig Topper <craig.topper at gmail.com> wrote:> I believe StringRef + const char* is overloaded at the bottom of Twine.h. > And without those overloads it might try to implicitly convert both > StringRef and const char* to Twine and then add them since StringRef + > const char * wouldn't otherwise be defined. > > inline Twine operator+(const Twine &LHS, const Twine &RHS) { > return LHS.concat(RHS); > } > > /// Additional overload to guarantee simplified codegen; this is > equivalent to > /// concat(). > > inline Twine operator+(const char *LHS, const StringRef &RHS) { > return Twine(LHS, RHS); > } > > /// Additional overload to guarantee simplified codegen; this is > equivalent to > /// concat(). > > inline Twine operator+(const StringRef &LHS, const char *RHS) { > return Twine(LHS, RHS); > } > > > > ~Craig > > > On Mon, Apr 29, 2019 at 3:18 PM Russell Wallace via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> I'm looking at the documentation on Twine at >> http://llvm.org/docs/ProgrammersManual.html#llvm-adt-twine-h and it >> gives example code: >> >> void foo(const Twine &T); >> ... >> StringRef X = ... >> unsigned i = ... >> foo(X + "." + Twine(i)); >> >> How exactly does that last line work? Since addition is left associative, >> I would expect it to be parsed as (X + ".") ... so it's trying to add a >> StringRef and a const char* before Twine can come into the picture at all? >> >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190429/afa551f4/attachment.html>
Seemingly Similar Threads
- [LLVMdev] Correct use of StringRef and Twine
- [LLVMdev] Correct use of StringRef and Twine
- Redundant Twine->StringRef->Twine conversions in llvm::sys::fs::make_absolute?
- [LLVMdev] Correct use of StringRef and Twine
- [LLVMdev] Correct use of StringRef and Twine