How can I check that instances of MyDouble
will be created at compile time?
What will happen if I instantiate MyDouble
with a non-constant expression?
#include <iostream>
struct MyDouble{
double myVal;
constexpr MyDouble(double v): myVal(v){}
constexpr double getVal(){ return myVal; }
};
int main() {}
There is no standard way to determine if a
constexpr
will be evaluated at compile-time or run-time. You can either inspect the assembly, follow the implementation-specific guidelines or try to speculate.However, using C++20 you can force your existing
constexpr
s to be evaluated at compile-time, or get an error if there is no such possibility. The same logic can act as a test for you.And then: