I have code equivalent to the following to print out a short string:
#include <iostream>
#include <string>
int main(int argc, const char* argv[])
{
std::string s = "finished??/not finished??";
std::cout << s << std::endl;
return 0;
}
But the output is appearing across two lines and losing some characters:
finished
ot finished??
But /n
isn't the new line character! What's happening?
In the first phase of translation (§2.2/1 of ISO/IEC 14882:2011(E)), sequences of characters known as trigraph sequences are replaced with single characters.
One of the trigraphs maps
??/
to\
. After the first phase, the code is equivalent to:As a result of the preprocessing phases,
"finished\not finished??"
is parsed as a string literal containing the escape-sequence\n
which represents the new line character. The outputted string is therefore:finished<NL>ot finished??
.To avoid this, you need to escape one of the question marks as
\?
. This gives you:This avoids the
??/
being picked up as a trigraph.