It is开发者_如何学Go 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.
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.
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.
精彩评论