Some values are known before the program runs. A fixed shelf arrangement, for example, need not wait until runtime to determine its capacity. constexpr marks a variable whose initializer must be a constant expression. It can also mark a function that is eligible for constant evaluation when the arguments and context permit it.
#include <array>
#include <iostream>
constexpr int trays(int rows) {
return rows * 2;
}
int main() {
constexpr int count = trays(3);
std::array<int, count> labels = {};
std::cout << labels.size() << '\n';
}
The array's size is part of its type, so it needs a compile-time value. count satisfies that requirement. Empty braces initialize all the integer elements to zero; the program does not read uninitialized storage.
A constexpr function is still a function. It can receive a runtime argument and then be evaluated at runtime. The keyword does not mean that every call must happen during compilation. For this lesson, the supplied arguments are constants and the result initializes a constexpr variable, so constant evaluation is required.
The editor contains a safe placeholder calculation that returns zero. A zero-length std::array is valid; the probes only inspect its size. You will implement the capacity rule and then select a different fixed number of shelves. The first array gets its size from capacity(3). The second uses secondShelves * 4 directly. This lets you check the function and the shelf setting separately. Keep the functions small and free of output: printing is a runtime activity, while the capacity calculation only needs its inputs.
Edit main.cpp; the supplied Console probes already call your code.
capacity as four places per shelf. Expect First capacity: 12.constexpr value secondShelves to five. Expect Second capacity: 20.Use Run to inspect the Console, then Submit to check your work.