Reputation: 119
I am experimenting a little bit with the const...
specifiers. I have written the following code, in which I use constexpr
for two arrays:
class MyClass{
private:
static constexpr std::array<uint64_t, 5> arr = {0,1,2,3,4};
static constexpr std::array<uint64_t, 5> arr_sum {[](const std::array<uint64_t, 8> & arr) {
std::array<uint64_t, 5> arr_sum;
arr_sum[0] = arr[0];
for (int i = 1; i < 5; i++) {
arr_sum[i] = arr[i] + arr_sum[i-1];
}
return arr_sum;
}(arr)};
public:
...
};
Where the array arr_sum
stores the sum of the first i
elements in the i
position.
As far as I know, with constexpr
the compiler tries to make arr
and arr_sum
at compilation time. But, there is also consteval
and constinit
. The first one ensures that a function is evaluated at compilation time, and the second one ensures constant initialization of a static or thread-local variables.
I have two questions here:
is there any chance that the arrays arr
or arr_sum
are not created at compilation time despite using constexpr
?
can I ensure compilation time creation of both arrays using the consteval
or constinit
specifiers?
Upvotes: 5
Views: 146