views:

251

answers:

2
+2  Q: 

C++ Template Quine

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.

+5  A: 

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.

Potatoswatter
@Jerry: adjusted.
Potatoswatter
@Potatoswatter:comment likewise.
Jerry Coffin
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
+3  A: 

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.

Jerry Coffin