2

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.

5
  • Other than inspection of the generated code, there's no way to tell. Commented Sep 19, 2020 at 15:55
  • you will end up in creating a source code analyzer like ANTLR or other grammer based lexical tools, it is quite difficult to assume the #define macros etcc.., Commented Sep 19, 2020 at 15:57
  • static and constexpr expressions must be evaluated at compile time. Other than that, your compiler might choose to evaluate more things at compile time for optimizations sake. Like @JesperJuhl said, there is no way to tell other than inspecting the specific code. Commented Sep 19, 2020 at 15:58
  • 3
    @IradOhayon "constexpr expressions must be evaluated at compile time" - Not true. They may be evaluated at compile time, but they may also be evaluated at runtime. Commented Sep 19, 2020 at 16:00
  • 1
    @IradOhayon constexpr is not a requirement that something be evaluated at compile-time. It's merely a hint/request that "if this thing is used with constants, please evaluate it at compile-time" - but the compiler is well within its right to ignore that, and if you don't call the thing with constants it has to ignore you. Commented Sep 19, 2020 at 16:12

1 Answer 1

3

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.

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. In other words, std::is_constant_evaluated() is required to give the right results regardless.

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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

as if + In other words, std::is_constant_evaluated() is required to give the right results regardless. isn't fully clear to me. Do I understand it right, that for std::is_constant_evaluated(X) the compiler is allowed to return true even if X wasn't actually precalculated during the compilation and is calculated on the fly at runtime, provided that it could had been precalculated but the compiler just decided not to?
is_constant_evaluated doesn't take an argument. The argument is actually the current evaluation context. And yes, the program is allowed to compute everything at run-time, so long as it pretends.
Right.. examples at en.cppreference.com/w/cpp/types/is_constant_evaluated are pretty clear, thanks. Especially the first one :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.