[英]How to tell which functions are evaluated at compilation time?
I'm developing a physics simulation program doing a lot of maths.我正在开发一个进行大量数学运算的物理模拟程序。 I'd like to know exactly which functions (or code parts) are evaluated at compilation time.
我想确切地知道在编译时评估了哪些函数(或代码部分)。
It there a way to tell?它有办法告诉吗? I'm using clang, usually with c++14.
我正在使用clang,通常使用c++14。
The language has precise rules about which expressions are evaluated at compile-time, and which are not.该语言对哪些表达式在编译时被求值、哪些不求值有精确的规则。 The technical term for that is constant-evaluated , and there is in fact a utility called
std::is_constant_evaluated
that can be used to programmatically query whether a particular evaluation of an expression must happen at compile-time or not.技术术语是constant-evaluated ,实际上有一个名为
std::is_constant_evaluated
的实用程序,可用于以编程方式查询表达式的特定计算是否必须在编译时发生。
However, the language is still subject to the as-if rule, which allows the implementation to actually produce a program that evaluates expressions whenever it wants, so long as the program behaves as-if the expressions are evaluated when they are supposed to.但是,该语言仍受as-if规则的约束,该规则允许实现实际生成一个程序,该程序可以在需要时对表达式进行计算,只要该程序的行为就像表达式在预期时进行计算一样。 In other words,
std::is_constant_evaluated()
is required to give the right results regardless.换句话说,
std::is_constant_evaluated()
都需要std::is_constant_evaluated()
给出正确的结果。
In practice, an implementation is allowed to produce a program that includes the entire compiler, and evaluates everything at run-time.在实践中,允许实现生成包含整个编译器的程序,并在运行时评估所有内容。 Or the implementation can calculate all the results that it possibly can, even if it doesn't have to, and store them directly into the program it produces.
或者实现可以计算它可能计算的所有结果,即使它不需要,并将它们直接存储到它生成的程序中。 Again, so long as the resulting program obeys the as-if rule, everything's ok.
同样,只要生成的程序遵守as-if规则,一切都会好起来的。
If you want to know which expressions were actually evaluated at compile-time, you'll need to look at the assembly of the particular program generated by the implementation you're using.如果您想知道在编译时实际计算了哪些表达式,您需要查看由您正在使用的实现生成的特定程序的程序集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.