<p>If your compiler can be made to accept <a href="http://en.wikipedia.org/wiki/C++11" rel="nofollow">C++11</a> standard, you could use <em>raw string literals</em> like eg:</p>

<pre><code>  std::cout &lt;&lt; R"*(&lt;!DOCTYPE html&gt;
       &lt;html&gt;
       &lt;head&gt;
       &lt;title&gt;Title with a backslash \ here 
     and double " quote&lt;/title&gt;)*";
</code></pre>

<p>Hence with raw string literals there is no forbidden sequence of characters in those raw string literals. Any sequence of characters could appear in them (but you can define the ending sequence of the raw string)</p>

<hr>

<p>And you could use <code>#{</code> and <code>}#</code> like I do in <a href="http://www.gcc-melt.org/tutomeltlang.html" rel="nofollow">MELT macro-strings</a>; <a href="http://gcc-melt.org/" rel="nofollow">MELT</a> is Lisp-like <a href="http://arxiv.org/abs/1109.0779" rel="nofollow">domain specific language</a> to extend GCC, and you can embed code in it with e.g.</p>

<pre><code>(code_chunk hellocount_chk
            #{ /* $HELLOCOUNT_CHK chunk */ 
                 static int $HELLOCOUNT_CHK#_counter; 
                 $HELLOCOUNT_CHK#_counter++;
               $HELLOCOUNT_CHK#_lab:
                 printf ("Hello World, counted %d\n", 
                         $HELLOCOUNT_CHK#_counter);
                 if (random() % 4 == 0) goto $HELLOCOUNT_CHK#_lab;
            }#)
</code></pre>

<p>The <code>#{</code> and <code>}#</code> are enclosing macro-strings (these character sequences are unlikely to appear in C or C++ code, except in string literals and comments), with the <code>$</code> starting symbols in such macro-strings (up to a non-letter or <code>#</code> character).</p>

<p><sup>Using <code>#{</code> and <code>}#</code> is not fool-proof (e.g. because of raw string literals) but good enough: a cooperative user could manage to avoid them.</sup></p>
Escaping blocks of foreign code in C++
<p>I'm currently working on a toy language that works like this: one can embed blocks written in this language into a C++ source, and before compilation, these blocks are translated into C++ in an extra preprocessing step, producing a valid C++ source.</p>

<p>I want to make sure that these blocks can always be identified in the source unambiguously and also, whenever such a block is present in the source, it cannot be valid C++. Moreover, I want to achieve these by putting as few constraints to the embedded language as possible (the language itself is still somewhat fluid).</p>

<p>The obvious way would be to introduce a pair of special multi-character parentheses, made of characters that cannot appear together in valid C++ code (or in the embedded language). However, I'm not sure how to ensure that particular a character sequence is good for this purpose (not after <a href="http://www.gotw.ca/gotw/078.htm" rel="nofollow">GotW #78</a>, anyway (: ).</p>

<p>So what is a good way to escape these blocks?</p>