[LLVMdev] Correct use of StringRef and Twine

David Blaikie dblaikie at gmail.com
Fri Jul 22 14:59:33 PDT 2011

> The dangerous part of this is that characters are integers, so "foo" + 'x' is very likely to cause serious problems.

std::string already provides such overloads though, doesn't it? So the
code isn't any safer from accidental "foo" + 'x' expressions that
don't include Twine/StringRef/std::string than it was before. But if
the argument is that std::string's interface was poorly
designed/unsafe & we can do better/safer, I'm OK with making the ctor
explicit as you've suggested.

> You should also probably add a ctor for signed/unsigned char as well (which reuse the existing CharKind enum).

Hmm - would it be safe to cast those signed/unsigned chars to straight
char? (is it guaranteed that the signed & unsigned values with the
same representation map to the same glyph?)

As a side note on Twine's design: Is there a particular reason it uses
void*s rather than unions? and chars rather than enums?

(sorry if I'm asking lots of "why is this like this" questions all
over the code base - I just don't want to assume that it's intentional
and replicate a pattern elsewhere that I don't understand only to find
it's unintentional "not fixed yet" sort of stuff. I suppose at the
very least it'll be a chance to add in some explanatory comments if I
do find things that are by design but weren't clear to me)

- David

More information about the llvm-dev mailing list