Template Metaprogramming 04 - Raison D'etre of constexpr

preview_player
Показать описание
constexpr is introduced to C++11

it is not meant for const expression
it is for template metaprogramming.

constexpr is a keyword to instruct
the C++ compiler to regard expression as compile-time code

Entities decorated with constexpr as in

constexpr size_t size = 10;

get removed from the run-time code as preprocessor macros are removed after preprocessing stage.

Since they do not exist at run-time, we cannot change their value at run-time.

If we cannot change them at run-time?
What are they useful for?

constexpr is not meant for expressions such as

constexpr size_t size = 10;

constexpr is designed for variable templates as in

template - typename T -
constexpr size_t size = st_type_helper T :: value;

The variable template size decorated with constexpr does change its value depending on the type T. This is why they are called variable templates. And these variable templates play a very important role in Template Metaprogramming.
Рекомендации по теме