constexpr计算量的实际限制 [英] Practical limitations on amount of constexpr computation

查看:107
本文介绍了constexpr计算量的实际限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个实验,我只是整理了一些代码以在编译时生成std::array<uint32_t, 256>.表内容本身是一个非常典型的CRC查找表-唯一的新事物是使用constexpr函数来计算条目,而不是将自动生成的魔术表直接放在源代码中.

As an experiment, I just put together some code to generate a std::array<uint32_t, 256> at compile time. The table contents themselves are a fairly typical CRC lookup table - about the only new thing is the use of constexpr functions to calculate the entries as opposed to putting an autogenerated magic table directly in the source code.

无论如何,这个练习让我感到好奇:编译器愿意在编译时评估constexpr函数或变量定义的计算量是否存在任何实际限制?例如类似于gcc的-ftemplate-depth参数对模板元编程评估的数量产生了实际限制. (我还想知道,参数包的长度是否可能存在实际限制-这会限制使用std::integer_sequence中间对象创建的编译时std::array的大小.)

Anyway, this exercise got me curious: would there be any practical limitations on the amount of computation a compiler would be willing to do to evaluate a constexpr function or variable definition at compile time? e.g. something like gcc's -ftemplate-depth parameter creating practical limits on the amount of template metaprogramming evaluation. (I also wonder if there might be practical limitations on the length of a parameter pack - which would limit the size of a compile-time std::array created using a std::integer_sequence intermediate object.)

推荐答案

对此的建议可以在

(2.35)  —  递归constexpr函数调用[ 512 ]

(2.36)  —  在核心常量表达式[ 1 048 576 ]

(2.36)   —   Full-expressions evaluated within a core constant expression [1 048 576]

GCC和Clang允许通过-fconstexpr-depth(这是您要查找的标志)进行调整.

GCC and Clang allow adjustment via -fconstexpr-depth (which is the flag you were looking for).

常量表达式评估实际上在沙箱中运行,因为未定义的行为必须先被实施.考虑到这一点,我不明白为什么实现无法使用主机的全部资源.再说一次,我不建议编写编译需要千兆字节内存或其他不合理资源的程序...

Constant expression evaluation practically runs in a sandbox, because undefined behavior must be preempted by the implementation. With that in mind, I don't see why the implementation couldn't use the entire resources of the host machine. Then again, I wouldn't recommend writing programs whose compilation requires gigabytes of memory or other unreasonable resources...

这篇关于constexpr计算量的实际限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆