Skip to content

constexpr (C++11)

In a Nutshell

Tells the compiler "this value or function can be evaluated at compile time," shifting runtime computations to the compile phase and achieving zero-overhead complex logic.

None (language keyword)

Core API Quick Reference

OperationSignatureDescription
Compile-time variableconstexpr T var = expr;Requires expr to be a constant expression; the variable is implicitly const
Compile-time functionconstexpr T func(params);If arguments are constants, it evaluates at compile time; otherwise, it degrades to a normal function
Compile-time constructionconstexpr T::T(params);Allows constructing literal type objects in constant expressions
Compile-time destructionconstexpr T::~T();(C++20) Allows destroying objects in constant expressions
Feature test macro__cpp_constexprDetects the current compiler's level of constexpr support

Minimal Example

cpp
// Standard: C++14
#include <iostream>

constexpr int factorial(int n) {
    int res = 1;
    while (n > 1) res *= n--;
    return res;
}

int main() {
    constexpr int val = factorial(5); // 编译期计算
    std::cout << val << '\n';         // 输出: 120
    int k = 4;
    std::cout << factorial(k) << '\n';// 运行期计算: 24
}

Embedded Applicability: High

  • Moves computations like table lookups, CRC checks, and protocol parsing to compile time, saving Flash/RAM space
  • Compile-time computed values can be used directly as template parameters (e.g., array sizes), meeting the static configuration needs of bare-metal environments
  • Offers better readability and debugging experience compared to C macros and template metaprogramming
  • Note that C++11 has many restrictions (single return statement); we recommend embedded projects use at least the C++14 standard

Compiler Support

GCCClangMSVC
4.63.119.0

See Also


Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0

Built with VitePress