constexpr计算量的实际限制 [英] Practical limitations on amount of constexpr computation
问题描述
作为一个实验,我只是整理了一些代码以在编译时生成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允许通过 GCC and Clang allow adjustment via 常量表达式评估实际上在沙箱中运行,因为未定义的行为必须先被实施.考虑到这一点,我不明白为什么实现无法使用主机的全部资源.再说一次,我不建议编写编译需要千兆字节内存或其他不合理资源的程序... 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屋!-fconstexpr-depth
(这是您要查找的标志)进行调整.-fconstexpr-depth
(which is the flag you were looking for).