C++ examples for Data Type:constexpr
Your compiler will throw an error if you add any code to them that prevents compile time evaluation.
The following code uses a constexpr variable to define the size of an array.
#include <array> #include <cstdint> #include <iostream> int main()/*from w ww. j av a 2s .co m*/ { constexpr int ARRAY_SIZE{ 5 }; std::array<int, ARRAY_SIZE> myArray{ 1, 2, 3, 4, 5 }; for (auto&& number : myArray) { std::cout << number << std::endl; } return 0; }