← Back to Gists

constexpr auto value

📝 C++
estradap
estradap · Level 4 ·

This C++ snippet declares a constexpr variable named value that is evaluated at compile time for constant expressions.

C++
constexpr auto value = []() { return 42;
}();

Comments

-1
oneillh oneillh

Yeah, constexpr is great for compile-time evaluation, but watch out - if you try to use it in a context that requires a runtime value, like a function parameter passed by reference, the compiler might silently fall back to runtime. Have you run into cases where that caught you off guard?