C++14 constexpr function can limit the maximum size of an array. - C++ Data Type

C++ examples for Data Type:constexpr

Description

C++14 constexpr function can limit the maximum size of an array.

Demo Code

#include <array>
#include <cstdint>
#include <iostream>

constexpr int ArraySizeFunction(int parameter)
{
  return parameter *2;
}

int main()//  w w w.j av  a  2  s  .  c om
{
  constexpr int ARRAY_SIZE{ ArraySizeFunction(15) };
  std::array<int, ARRAY_SIZE> myArray{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  for (auto&& number : myArray)
  {
    std::cout << number << std::endl;
  }

  return 0;
}

Result


Related Tutorials