作为语言特征的静态数据定义

发布于 2024-10-22 22:29:32 字数 474 浏览 8 评论 0原文

我正在用 C++ 编写一个程序,其中使用一些常量数据。我在启动时按程序构建该数据,并且不再更改它。我知道理想情况下数据应该是二进制文件中的静态数据,但事实并非如此,因为我必须首先构建它们。

在 C++ 中,我可以使用该数据或简单的全局 const 在类中定义静态 const 成员。通过这样做,我将在二进制文件中拥有静态数据,但随后我将无法对其内容进行编程。我必须在其他地方构建它们并将结果粘贴到代码中。就我而言,这将是一堆丑陋且毫无意义地编码在源文件内的二进制数据。

所以我开始想知道,是否有任何语言支持这种功能,使我能够按程序定义静态数据,但在编译时解析它并嵌入到二进制文件中?任何类型的优化都可以处理这些情况吗?如果生成数据的过程/函数不采用外部参数并且具有可预测的结果,则编译器可以使用结果安全地对其进行优化。编译器会走这条路吗?您知道的任何语言都明确处理这个问题吗?

我知道 C 预处理器,但它确实与图灵不兼容,并且它的语法不如告诉它应该在编译时解析的函数修饰符那么有吸引力。

I am writing a program in C++ in what I use some constant data. I build that data procedurally at startup, and never again change it. I know that ideally that data should be static data at the binaries, but that is not the case since I have to build them at first.

In C++ I could define static consts members in classes with that data or simply global consts. By doing this I would have static data in the binaries, but then I would not be able to program their contents. I would have to build them elsewhere and paste the result in the code. In my case it would be a bunch of binary data uglily and nonsenselesly encoded inside the source file.

So I started to wonder, is there any language that support such feature that enables me to define my static data procedurally, but that resolves it at compile time and embed inside the binaries? Could any kind of optimization handle these cases? If the procedure/function that generates the data takes no external parameters and has predictable results, it could be safely optimized with the results by the compiler. Does compilers take that path? Any languages you know does treat explicitly this matter?

I know of C preprocessor, but it is really not Turing-compatible, and its syntax is not as attractive as it would be a function modifier that tells it should be resolved at compile time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

谁把谁当真 2024-10-29 22:29:32

构建一个程序来生成数据(您已经拥有了该程序),使其输出为 C++ 格式,将生成添加到您的 Makefile 中,并通过 #include 使用生成的数据。

Build a program to generate the data (you already have that), have it's output be in C++, add the generation to your Makefile, and use the generated data with #include.

疑心病 2024-10-29 22:29:32

C++ 模板是图灵完备的,通常用于计算编译时常量。

C++ templates are Turing-complete, and are often used to compute compile-time constants.

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