It is known that C++ templates are turing complete. As such it should be possible to output a quine that is essentially rendered at compile time. Does anyone know if such a quine has been written yet or where I could find one.
-
But where do you want to get the output? – kennytm Feb 15 '10 at 19:39
-
1First, I'm not sure that Turing completeness is a necessary requirement for a quine. Second, I AM sure that OUTPUT is a necessary requirement for a quine. Last I checked, templates don't have any output, per se. – Cogwheel Feb 15 '10 at 19:41
-
5Actually, I'm quite sure that turing completeness is unnecessary. I could define a language with a single operation, ">" that does nothing but output ">". Every program in this language would be a quine. – Cogwheel Feb 15 '10 at 19:42
-
@Cogwheel: I don't believe he is saying it's necessary, just making an off-the-cuff remark that given template metaprogramming is Turing-complete, it's then possible to write a quine. (I'm not sure this is true, but that's how I read the question.) – Feb 15 '10 at 19:43
-
1@Cogwheel: You just described HQ9+ :) – kennytm Feb 15 '10 at 19:46
-
He wasn't saying it's necessary, per se, but he did use Turing completeness as the entire premise that a quine should be possible. I'm simply trying to illustrate why this is incorrect. The point about output in my original comment is arguably more important. My second comment was about making my first comment more accurate than trying to further the point I was making :) – Cogwheel Feb 15 '10 at 19:48
-
2The output should be a compiler error message ;) – UncleBens Feb 15 '10 at 19:57
-
I thought turing completeness was a sufficient (if not neccessary) condition for the ability to produce a quine. As far as the output problem, I hadn't thought about this. – Jeff Feb 15 '10 at 20:46
-
@KennyTM: but HQ9+ is 4x as big as the language @Cogwheel described! – SamB Apr 15 '10 at 02:22
2 Answers
Templates can perform any kind of computation on integer data elements, true. But they aren't so good at I/O.
What form should the answer take? A template that generates a function that, when executed, outputs the quine source? That's not really compile time. A template that generates a compile-time list of characters (hundreds or thousands of classes long) composing quine source? Maybe that's better, but you still need to run the program to output it.
Also, templates are very verbose, and although they are turing complete, that is only within a small memory constraint guaranteed recommended by the standard. You can only expect so much recursion, for example, beyond which the program is highly compiler-specific. It might be impossible to write a "meaningfully computed" quine which stores itself in a portable form.

- 134,909
- 25
- 265
- 421
-
I like your suggestion for the output, a compile-time list of characters that is identical to the original source. This list of characters would not be outputted in the traditional sense but would just be embedded into the compiled executable as a string literal. Anyways, I was just curious if anyone had attempted such a thing. – Jeff Feb 15 '10 at 20:57
Templates have only one form of direct output -- error/warning messages. Since there's no guarantee about the form these take, you can't write anything that's certain to be a quine, and whatever you write will almost certainly have other text interspersed with the source code.
With a compiler that embeds the source in the error message, getting every line output is all too easy -- just ensure that every statement contains an error.

- 476,176
- 80
- 629
- 1,111