When does a constexpr function get evaluated at compile time?
constexpr
functions will be evaluated at compile time when all its arguments are constant expressions and the result is used in a constant expression as well. A constant expression could be a literal (like 42
), a non-type template argument (like N
in template<class T, size_t N> class array;
), an enum
element declaration (like Blue
in enum Color { Red, Blue, Green };
, another variable declared constexpr, and so on.
They might be evaluated when all its arguments are constant expressions and the result is not used in a constant expression, but that is up to the implementation.
When is a constexpr evaluated at compile time?
When a constexpr
function is called and the output is assigned to a constexpr
variable, it will always be run at compiletime.
Here's a minimal example:
// Compile with -std=c++14 or later
constexpr int fib(int n) {
int f0 = 0;
int f1 = 1;
for(int i = 0; i < n; i++) {
int hold = f0 + f1;
f0 = f1;
f1 = hold;
}
return f0;
}
int main() {
constexpr int blarg = fib(10);
return blarg;
}
When compiled at -O0
, gcc outputs the following assembly for main
:
main:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 55
mov eax, 55
pop rbp
ret
Despite all optimization being turned off, there's never any call to fib
in the main
function itself.
This applies going all the way back to C++11
, however in C++11 the fib
function would have to be re-written to use conversion to avoid the use of mutable variables.
Why does the compiler include the assembly for fib
in the executable sometimes? A constexpr
function can be used at runtime, and when invoked at runtime it will behave like a regular function.
Used properly, constexpr
can provide some performance benefits in specific cases, but the push to make everything constexpr
is more about writing code that the compiler can check for Undefined Behavior.
What's an example of constexpr
providing performance benefits? When implementing a function like std::visit
, you need to create a lookup table of function pointers. Creating the lookup table every time std::visit
is called would be costly, and assigning the lookup table to a static
local variable would still result in measurable overhead because the program has to check if that variable's been initialized every time the function is run.
Thankfully, you can make the lookup table constexpr
, and the compiler will actually inline the lookup table into the assembly code for the function so that the contents of the lookup table is significantly more likely to be inside the instruction cache when std::visit
is run.
Does C++20 provide any mechanisms for guaranteeing that something runs at compiletime?
If a function is consteval
, then the standard specifies that every call to the function must produce a compile-time constant.
This can be trivially used to force the compile-time evaluation of any constexpr function:
template<class T>
consteval T run_at_compiletime(T value) {
return value;
}
Anything given as a parameter to run_at_compiletime
must be evaluated at compile-time:
constexpr int fib(int n) {
int f0 = 0;
int f1 = 1;
for(int i = 0; i < n; i++) {
int hold = f0 + f1;
f0 = f1;
f1 = hold;
}
return f0;
}
int main() {
// fib(10) will definitely run at compile time
return run_at_compiletime(fib(10));
}
c++: constexpr function doesn't evaluate at compile time when using with std::cout
As I realize constexpr functions are evaluated at compile time
Not really. They can be evaluated at compile-time, but they are not guaranteed to do so, unless they're invoked in a context where a constant expression is required.
One such context is the declaration of a constexpr
variable.
C++ constexpr - Value can be evaluated at compile time?
The quoted wording is a little misleading in a sense. If you just take PlusOne
in isolation, and observe its logic, and assume that the inputs are known at compile-time, then the calculations therein can also be performed at compile-time. Slapping the constexpr
keyword on it ensures that we maintain this lovely state and everything's fine.
But if the input isn't known at compile-time then it's still just a normal function and will be called at runtime.
So the constexpr
is a property of the function ("possible to evaluate at compile time" for some input, not for all input) not of your function/input combination in this specific case (so not for this particular input either).
It's a bit like how a function could take a const int&
but that doesn't mean the original object had to be const
. Here, similarly, constexpr
adds constraints onto the function, without adding constraints onto the function's input.
Admittedly it's all a giant, confusing, nebulous mess (C++! Yay!). Just remember, your code describes the meaning of a program! It's not a direct recipe for machine instructions at different phases of compilation.
(To really enforce this you'd have the integer be a template argument.)
Force a constexpr function to be compiled at compile time, even if calculation inside contains a non-const array, by making the returned obj constant?
From what i know, a constexpr function is definitely evaluated at compile time, when every value inside the function is const and used functions are constexpr which also only use const values. Furthermore, an other post stated, that it's possible to force a compile time compilation by making the returned obj const.
All of these statements are wrong. See below.
No, a call to a constexpr
function is only guaranteed to be evaluated at compile-time if it is called in a context requiring a (compile-time) constant expression. The initializer of obj2
is not such a context, even if it is const
.
You can force the initializer to be compile-time evaluated by declaring obj2
as constexpr
. (Which however has very different meaning than const
!)
Even then it is not going to work, because calculations<double, size>(obj1)
is not actually a constant expression. obj1
is not a compile-time constant without declaring it constexpr
as well. Similarly this doesn't work because test1
is not a constant expression without declaring it constexpr
as well.
Then you also need to make the constructor of Array
constexpr
and you need to actually fill the values of test1
inside calculations
, because accessing uninitialized values causes undefined behavior and undefined behavior makes expressions not constant expressions.
So all in all:
template<int Size, typename T>
struct Array{
T array[Size];
constexpr Array(const T * a) {
for(int i = 0; i < Size; i++){
array[i] = a[i];
}
}
};
template<typename T, int size>
class Example{
private:
Array<size, T> _array;
public:
constexpr explicit Example(T * arr):_array(arr){};
constexpr explicit Example(const T * arr):_array(arr){};
};
template<typename D, int size, typename ...buf, typename T>
constexpr auto calculations(const T & myObj){
D test1[2];
test1[0] = 0;
test1[1] = 1;
// calculation fills arr
return Example<D, size>(test1);
}
int main(){
const int size = 2;
constexpr double test1[size] = {1,2};
constexpr auto obj1 = Example<double, size>(test1); //compile time
//obj2 calculations during compile time or run-time?
constexpr auto obj2 = calculations<double, size>(obj1);
}
In C++20 there will be an alternative keyword consteval
which one can use instead of constexpr
on a function to force it to always be evaluated at compile-time. Currently there is no way to do that without making e.g. the destination of the return value a constexpr
variable.
In fact your original code has undefined behavior. Because Array
does not have a constexpr
constructor, objects of that type can never be constructed in constant expressions. And because Example
uses that type, it cannot be used in constant expressions either. This makes it illegal to put constexpr
on its constructor, because a function declared constexpr
causes undefined behavior if there isn't at least one valid set of template arguments and function arguments that would produce a constant expression. (The same then applies to calculations
as well, because it uses Example
.
So you must put constexpr
on the constructor of Array
in any case if your program is supposed to be well-formed.
Whether variables created inside the constant expression (e.g. inside calculations
) are const
does not matter at all.
constexpr function contains a const - will it ever be evaluated at compile time?
Your code is ill-formed, no diagnostic required. In
constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; }
There is no input which will ever make the function a core constant expression which is specified by [dcl.constexpr]/5
For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, or, for a constructor, a constant initializer for some object ([basic.start.static]), the program is ill-formed, no diagnostic required.
Related Topics
How to Use Sdl2 in My Programs Correctly
Convert Python Program to C/C++ Code
How to "Return an Object" in C++
Legality of Cow Std::String Implementation in C++11
Does the Size of an Int Depend on the Compiler And/Or Processor
What Does the Question Mark Character ('') Mean in C++
Converting Std::_Cxx11::String to Std::String
How to Use the Conditional (Ternary) Operator
How to Clear a Stringstream Variable
Static Variables Initialisation Order
How to Terminate a Thread in C++11
C++ Std::Set Update Is Tedious: I Can't Change an Element in Place