Explain the difference between constexpr, consteval, and constinit. When would you use one over the others?

Last updated 21 June 2026

  • constexpr means "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.

  • https://utkrusht.ai/blog/cplusplus-interview-questions
  • https://roadmap.sh/questions/cpp
  • https://internshala.com/blog/cpp-coding-interview-questions/
All C++ interview questions