constexpr 计算量的实际限制
Practical limitations on amount of constexpr computation
作为实验,我只是将一些代码放在一起以在编译时生成 std::array<uint32_t, 256>
。 table 内容本身是一个相当典型的 CRC 查找 table - 唯一的新事物是使用 constexpr
函数来计算条目,而不是放置自动生成的魔法 table直接在源代码中。
无论如何,这个练习让我感到好奇:编译器愿意在编译时评估 constexpr
函数或变量定义的计算量是否有任何实际限制?例如类似于 gcc 的 -ftemplate-depth
参数对模板元编程评估的数量产生了实际限制。 (我还想知道参数包的长度是否存在实际限制 - 这会限制使用 std::integer_sequence
中间对象创建的编译时 std::array
的大小。)
可在 [implimits] ¶2:
中找到此类建议
(2.35) — Recursive constexpr function invocations [512]
(2.36) — Full-expressions evaluated within a core constant expression [1 048 576]
GCC 和 Clang 允许通过 -fconstexpr-depth
(您正在寻找的标志)进行调整。
常量表达式计算实际上在沙箱中运行,因为 undefined behavior must be preempted by the implementation。考虑到这一点,我不明白为什么实施不能使用主机的全部资源。再一次,我不建议编写编译需要千兆字节内存或其他不合理资源的程序...
作为实验,我只是将一些代码放在一起以在编译时生成 std::array<uint32_t, 256>
。 table 内容本身是一个相当典型的 CRC 查找 table - 唯一的新事物是使用 constexpr
函数来计算条目,而不是放置自动生成的魔法 table直接在源代码中。
无论如何,这个练习让我感到好奇:编译器愿意在编译时评估 constexpr
函数或变量定义的计算量是否有任何实际限制?例如类似于 gcc 的 -ftemplate-depth
参数对模板元编程评估的数量产生了实际限制。 (我还想知道参数包的长度是否存在实际限制 - 这会限制使用 std::integer_sequence
中间对象创建的编译时 std::array
的大小。)
可在 [implimits] ¶2:
中找到此类建议(2.35) — Recursive constexpr function invocations [512]
(2.36) — Full-expressions evaluated within a core constant expression [1 048 576]
GCC 和 Clang 允许通过 -fconstexpr-depth
(您正在寻找的标志)进行调整。
常量表达式计算实际上在沙箱中运行,因为 undefined behavior must be preempted by the implementation。考虑到这一点,我不明白为什么实施不能使用主机的全部资源。再一次,我不建议编写编译需要千兆字节内存或其他不合理资源的程序...