C++模板奎因

发布于 2024-08-21 19:44:32 字数 77 浏览 7 评论 0原文

众所周知,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2024-08-28 19:44:32

模板可以对整数数据元素执行任何类型的计算,这是真的。但他们不太擅长 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 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.

回忆追雨的时光 2024-08-28 19:44:32

模板只有一种形式的直接输出——错误/警告消息。由于无法保证这些形式所采用的形式,因此您无法编写任何肯定是奎因的内容,并且您编写的任何内容几乎肯定会在源代码中散布其他文本。

使用将源代码嵌入到错误消息中的编译器,获取每一行输出都非常容易——只需确保每个语句都包含错误即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文