How does Twine work?

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?

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);
}

Ah, that makes sense. Thanks!