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.
Trigraph sequences (2.4) are replaced by corresponding single-character internal representations.
One of the trigraphs maps ??/
to \
. After the first phase, the code is equivalent to:
#include <iostream>
#include <string>
int main(int argc, const char* argv[])
{
std::string s = "finished\not finished??";
std::cout << s << std::endl;
return 0;
}
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:
#include <iostream>
#include <string>
int main(int argc, const char* argv[])
{
std::string s = "finished?\?/not finished??";
std::cout << s << std::endl;
return 0;
}
This avoids the ??/
being picked up as a trigraph.