Use Compile Time Constants with constexpr to guarantee that an expression can be evaluated at compile time. - C++ Data Type

C++ examples for Data Type:constexpr

Introduction

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.

Demo Code

#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;
}

Result


Related Tutorials