using my_array = std::array<char, 16384>;
void compare(const my_array* a) {
//...
char b[20000];
//...
static_assert(a->size() < sizeof(b)/sizeof(b[0]), "text"); //<compile time error: static_assert expression is not an integral constant expression
strlcpy(b, a->data(), a->size());
//...
}
my_array A;
compare(&A);
Size of all objects of type my_array
is known at compile time.
Unfortunately its not reasonable to copy them by value to each function, which is going to use it. Reference is forbidden here also, because it may allocate a pointer, which here is the problem.
But I would like to have a compile time checks as in the example above, because logically I don't see a reason, why I can't: the type is known, the size is defined.
This variant doesn't work: static_assert(my_array::size() < 20000, "text");
, obviously, because std::array<...>::size()
is not static.
How can I do this check?