{"id":1089,"date":"2013-07-02T01:00:00","date_gmt":"2013-07-01T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1089"},"modified":"2013-07-02T22:25:04","modified_gmt":"2013-07-02T21:25:04","slug":"c11-quine","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1089","title":{"rendered":"C++11 Quine"},"content":{"rendered":"<p>A <a href=\"http:\/\/en.wikipedia.org\/wiki\/Quine_(computing)\">Quine<\/a> is a program that takes no inputs and produces its source as its output. \u00e2\u20ac\u0153No inputs\u00e2\u20ac\u009d includes its source. They are an interesting little computational trick, and it takes a while to understand what\u00e2\u20ac\u2122s happened. For a long time, C and C++ quines have been among the more complicated because they have never supported any kind of delimited string input. Producing a quine was still possible, but it wasn\u00e2\u20ac\u2122t pretty because of all the escapes needed. With C++11, that limitation has been removed thanks to it\u00e2\u20ac\u2122s support for raw strings.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> raw_string = R<span class=\"st\">&quot;(_RAW_STRING_GOES_HERE_)&quot;<\/span>;<\/code><\/pre>\n<p>The key feature of a raw string is that it is delimited by more than just a quote. The delimiters can, in fact, be any arbitrary string of the forms:<\/p>\n<pre><code>R&quot;_arbitrary_string_(<\/code><\/pre>\n<p>i.e. <code>R&quot;<\/code> is always present, and <code>(<\/code> is always present, whatever is in between may be chosen by you at will to not clash with the content you wish to delimit. The end delimiter is similar:<\/p>\n<pre><code>)_arbitrary_string_&quot;;<\/code><\/pre>\n<p>Inside a raw string, backslash escapes are not processed, and of course quotes are just quotes.<\/p>\n<p>That makes it possible to make an educationally clear quine in C++11. I\u00e2\u20ac\u2122d like to do more than just give you one though; I\u00e2\u20ac\u2122d like to build up to one. Here\u00e2\u20ac\u2122s what we\u00e2\u20ac\u2122d like:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">int<\/span> main()\n{\n    cout &lt;&lt; THIS_PROGRAM &lt;&lt; endl;\n}<\/code><\/pre>\n<p>This is obviously incomplete, since <code>THIS_PROGRAM<\/code> is undefined. It\u00e2\u20ac\u2122s what we\u00e2\u20ac\u2122re aiming for though. Hopefully you can see the difficulty, a naive definition of <code>THIS_PROGRAM<\/code> won\u00e2\u20ac\u2122t work:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> THIS_PROGRAM = <span class=\"st\">&quot;int main() { cout &lt;&lt; THIS_PROGRAM &lt;&lt; endl;}&quot;<\/span>;\n\n<span class=\"dt\">int<\/span> main()\n{\n    cout &lt;&lt; THIS_PROGRAM &lt;&lt; endl;\n}<\/code><\/pre>\n<p>It doesn\u00e2\u20ac\u2122t work because <code>THIS_PROGRAM<\/code> doesn\u00e2\u20ac\u2122t contain the definition of <code>THIS_PROGRAM<\/code>. It should be obvious that continuing down this path will fail, everything we might add to <code>THIS_PROGRAM<\/code> will then be missing whatever we\u00e2\u20ac\u2122ve just added to <code>THIS_PROGRAM<\/code>.<\/p>\n<p>Let\u00e2\u20ac\u2122s instead think about the general form of a quine. It must be this:<\/p>\n<pre><code>THIS_PROGRAM = START_RAW + QUINE + END_RAW + QUINE<\/code><\/pre>\n<p>This is the core trick of a quine. There is <em>some<\/em> part of the text of the program, that here we\u00e2\u20ac\u2122ve name <code>QUINE<\/code>, that appears both inside a definition of a string, and outside. We are certain that this very line will be part of <code>QUINE<\/code>. That gives us somewhere to begin.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> QUINE = R<span class=\"st\">&quot;***(<\/span>\n<span class=\"co\">\/\/ QUINE will be above this line<\/span>\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> THIS_PROGRAM = BEFORE_QUINE + QUINE + AFTER_QUINE + QUINE;\n)***<span class=\"st\">&quot;;<\/span>\n<span class=\"co\">\/\/ QUINE will be above this line<\/span>\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> THIS_PROGRAM = BEFORE_QUINE + QUINE + AFTER_QUINE + QUINE;<\/code><\/pre>\n<p>This is obviously missing definitions of <code>BEFORE_QUINE<\/code> and <code>AFTER_QUINE<\/code>, but what goes in them <em>is<\/em> already defined, because we\u00e2\u20ac\u2122ve implicitly written them. We can add them easily, by observation:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> QUINE = R<span class=\"st\">&quot;***(<\/span>\n<span class=\"co\">\/\/ QUINE will be above this line<\/span>\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> BEFORE_QUINE = <span class=\"st\">&quot;auto const QUINE = R<\/span><span class=\"ch\">\\&quot;<\/span><span class=\"st\">***(&quot;<\/span>;\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> AFTER_QUINE = <span class=\"st\">&quot;)***<\/span><span class=\"ch\">\\&quot;<\/span><span class=\"st\">;&quot;<\/span>;\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> THIS_PROGRAM = BEFORE_QUINE + QUINE + AFTER_QUINE + QUINE;\n)***<span class=\"st\">&quot;;<\/span>\n<span class=\"co\">\/\/ QUINE will be above this line<\/span>\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> BEFORE_QUINE = <span class=\"st\">&quot;auto const QUINE = R<\/span><span class=\"ch\">\\&quot;<\/span><span class=\"st\">***(&quot;<\/span>;\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> AFTER_QUINE = <span class=\"st\">&quot;)***<\/span><span class=\"ch\">\\&quot;<\/span><span class=\"st\">;&quot;<\/span>;\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> THIS_PROGRAM = BEFORE_QUINE + QUINE + AFTER_QUINE + QUINE;<\/code><\/pre>\n<p>Note that every line we add inside <code>QUINE<\/code> gets added outside as well, so that the program always includes its own source. Note that the only thing missing from the definition of <code>QUINE<\/code> is the definition of <code>QUINE<\/code> itself, but that\u00e2\u20ac\u2122s fine because we\u00e2\u20ac\u2122re adding that on the fly in <code>THIS_PROGRAM<\/code>;<\/p>\n<p>It might not look it, but these few lines represent the job done, we have a program that stores its own text in the constant <code>THIS_PROGRAM<\/code>. It should be easy to see, that \u00e2\u20ac\u0153storing\u00e2\u20ac\u009d or \u00e2\u20ac\u0153outputting\u00e2\u20ac\u009d makes no major difference. Everything we do now is just to satisfy the language, and ensure all the newlines are correct.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"ot\">#include &lt;iostream&gt;<\/span>\n\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> QUINE = R<span class=\"st\">&quot;***(<\/span>\n<span class=\"co\">\/\/ QUINE will be above this line<\/span>\n\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> BEFORE_QUINE =\n    <span class=\"st\">&quot;#include &lt;iostream&gt;<\/span><span class=\"ch\">\\n\\n<\/span><span class=\"st\">&quot;<\/span>\n    <span class=\"st\">&quot;auto const QUINE = R<\/span><span class=\"ch\">\\&quot;<\/span><span class=\"st\">***(&quot;<\/span>;\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> AFTER_QUINE = <span class=\"st\">&quot;)***<\/span><span class=\"ch\">\\&quot;<\/span><span class=\"st\">;&quot;<\/span>;\n\n<span class=\"dt\">int<\/span> main()\n{\n    std::cout &lt;&lt; BEFORE_QUINE &lt;&lt; QUINE &lt;&lt; AFTER_QUINE &lt;&lt; QUINE;\n}\n)***<span class=\"st\">&quot;;<\/span>\n<span class=\"co\">\/\/ QUINE will be above this line<\/span>\n\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> BEFORE_QUINE =\n    <span class=\"st\">&quot;#include &lt;iostream&gt;<\/span><span class=\"ch\">\\n\\n<\/span><span class=\"st\">&quot;<\/span>\n    <span class=\"st\">&quot;auto const QUINE = R<\/span><span class=\"ch\">\\&quot;<\/span><span class=\"st\">***(&quot;<\/span>;\n<span class=\"dt\">auto<\/span> <span class=\"dt\">const<\/span> AFTER_QUINE = <span class=\"st\">&quot;)***<\/span><span class=\"ch\">\\&quot;<\/span><span class=\"st\">;&quot;<\/span>;\n\n<span class=\"dt\">int<\/span> main()\n{\n    std::cout &lt;&lt; BEFORE_QUINE &lt;&lt; QUINE &lt;&lt; AFTER_QUINE &lt;&lt; QUINE;\n}<\/code><\/pre>\n<p>Proof?<\/p>\n<pre><code>$ clang++ -ggdb3 -O2 -Wall -Wextra -std=c++11 -o quine quine.cc\n$ quine &gt; quine2.cc\n$ md5sum quine.cc quine2.cc\nd6353ab8c00324f4e905ad2bc54a8668  quine.cc\nd6353ab8c00324f4e905ad2bc54a8668  quine2.cc<\/code><\/pre>\n<p>Here\u00e2\u20ac\u2122s a fun party trick with a quine like this (it\u00e2\u20ac\u2122s not complicated, but it is strange):<\/p>\n<pre><code>$ clang++ -std=c++11 -o quine quine.cc\n$ rm -f quine.cc\n$ .\/quine | clang++ -std=c++11 -xc++ -o quine -\n$ .\/quine | clang++ -std=c++11 -xc++ -o quine -\n$ .\/quine | clang++ -std=c++11 -xc++ -o quine -\n$ .\/quine | clang++ -std=c++11 -xc++ -o quine -\n$ .\/quine | clang++ -std=c++11 -xc++ -o quine -<\/code><\/pre>\n<p>Compiling the output of a binary to make a new copy of the binary. Weird.<\/p>\n<p>This is actually the beginnings of a trick that <a href=\"http:\/\/www.evanjones.ca\/quines-trusting-trust.html\">Ken Thompson<\/a> talked about when discussing the issue of trustworthy compilers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Quine is a program that takes no inputs and produces its source as its output. \u00e2\u20ac\u0153No inputs\u00e2\u20ac\u009d includes its source. They are an interesting little computational trick, and it takes a while to understand what\u00e2\u20ac\u2122s happened. For a long time, C and C++ quines have been among the more complicated because they have never\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=1089\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[84,85,6],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1089"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1089"}],"version-history":[{"count":6,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1089\/revisions"}],"predecessor-version":[{"id":1103,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1089\/revisions\/1103"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}