I'm interested to know why the second static_assert
in my code below doesn't work. It seems like even though the array c
is a reference to a
, the size of the array is embedded in the type, so it should be available at compile time.
#include <array>
int main()
{
std::array<int,2> a = {1,2};
std::array<int,2> b = {2,3};
std::array<int,2>& c = a;
static_assert(a.size() == b.size(), "a.size==b.size"); // ok size of array is compile time constant
static_assert(c.size() == a.size(), "c.size==a.size"); // compiler error "static_assert expression is not an integral constant expression"
}
the size of the array is embedded in the type, so it should be available at compile time.
This is true. But regardless, c
is not a constant expression and therefore expression that contains it as a subexpression cannot be a constant expression - except certain operators that interact only with the type of the operand such as sizeof
.
You can get the size for example using:
static_assert(
std::tuple_size<
std::remove_reference_t<decltype(c)>
>::value == a.size(),
"c.size==a.size"
);
Unfortunately, it is not very pretty.