Explain the difference between constexpr, consteval, and constinit. When would you use one over the others?
Last updated
constexprmeans "can run at compile time" with runtime fallback, ideal for dual-use functions and values.consteval(C++20) means "must run at compile time": every call must yield a constant, used for validating literals or building tables.constinit(C++20) forces constant initialization of static/thread-local variables, avoiding the static initialization order fiasco, while keeping them mutable.