简体   繁体   English

如何判断哪些函数在编译时被评估?

[英]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.

相关问题 如何判断表达式是在编译时还是在运行时进行计算? - How to tell if expression is evaluated at compile time or runtime? 如何判断 `constexpr` 是否在编译时被评估(无需人工检查) - How to tell if `constexpr` is evaluated at compile time (without manual inspection) C++20 consteval 函数和 constexpr 变量 - 是否保证在编译时进行评估? - C++20 consteval functions and constexpr variables - are they guaranteed to be evaluated at compilation time? C++ 中的 sizeof 是在编译时还是运行时计算的? - Is sizeof in C++ evaluated at compilation time or run time? 如何在编译时选择方法? - How to select the method at compilation time? 如何找到UNIX编译时间 - How to find UNIX time of compilation 如何使用 GLM 减少编译时间? - How to reduce compilation time with GLM? 如何在编译期间输出诊断消息以判断调用了哪个重载函数 - How to ouput diagnostic messages during compiling time to tell which overloaded function was called 如何告诉CMake在编译特定版本的Qt期间使用? - How to tell CMake to use during compilation specific version of Qt? 如何在常量评估表达式中获得编译时错误? - How to get a compile time error in constant evaluated expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM