https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79452
Bug ID: 79452 Summary: Provide builtin to detect compile-time execution Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: gonzalobg88 at gmail dot com Target Milestone: --- The run-time performance of constexpr functions is often sub-optimal, because the optimal algorithm cannot be expressed within the capabilities of C++'s constexpr feature, and never will be (in the worst case, the optimal run-time implementation will require inline assembly, which cannot ever be standardized nor made constexpr). It would be very useful to have a builtin that allows detecting compile-time execution of the current function/block/scope like this: if constexpr(__ctfe) { // the outer scope is being executed at compile-time } else { // the outer scope will be executed at run-time } Implementation as a builtin is preferred, because it is possible that "if constexpr() { }" syntax will be proposed for standardization. I'd rather have this built-in always work. That is, even if its invoked inside a non-constexpr function, it might still be that some block inside that function is evaluated by the constant expression evaluator of the C++ compiler. The built-in should detect this case properly. There is an alternative proposal, using a function attribute [[constexpr_alias(constexpr_fn_decl)]]: constexpr int foo_constexpr(); [[constexpr_alias(foo_constexpr)]] int foo(); that will replace a call to foo by a call to foo_constexpr whenever foo is called during constant expression evaluation. Both features are isomorphic, and enable the same new kind of programs to be written. Only one of them should be implemented. It is still not clear which.