Micro-coding standard question

I recall from a while back that LLVM coding style preferred using a literal char instead of a single-character string when writing to an output stream. That is:

OS << ' ';  // Instead of OS << " ";
OS << '\n'; // Instead of OS << "\n";

Some of my code reviewers gave the same feedback as well, but this is not mentioned anywhere in the coding standard. I am assuming the only reason to prefer a character instead of a string is performance (else the string is more idiomatic). Currently the code has a mix of both, and I wanted to check if there is any reason to use single char over a string. In optimized builds, it would seem the performance is likely same.

We don’t need to document this necessarily in the CS but would be good to establish if there is a preference or both are acceptable, or maybe the preference is now to use the more idiomatic string.

Thanks

The single character calls a different overload of <<, which gets inlined but the string one does not

Unless the code is performance critical (which most stream printing is not, but there are some rare cases) my guideline would be to match the surrounding style:

Most uses of << are also going to involve the use some multi-character "string" literals, in which case I’d consistently use "string" literals for everything, even if use of character literals is possible in some cases. Basically, don’t mix " and ' for the sake of micro-optimization.

2 Likes