C++模板奎因
众所周知,C++ 模板是图灵完备的。因此,应该可以输出基本上在编译时渲染的 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模板可以对整数数据元素执行任何类型的计算,这是真的。但他们不太擅长 I/O。
答案应该采取什么形式?生成一个函数的模板,该函数在执行时输出 quine 源代码?这并不是真正的编译时间。一个生成组成 quine 源的编译时字符列表(数百或数千个类长)的模板?也许这样更好,但你仍然需要运行程序来输出它。
此外,模板非常冗长,尽管它们是图灵完备的,但这仅在标准推荐的
保证的小内存限制范围内。例如,您只能期望这么多的递归,超出这个范围,程序就高度特定于编译器。编写一个以可移植形式存储自身的“有意义计算的”quine 可能是不可能的。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
guaranteedrecommended 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.