Let's say I have a function like:
int test(std::array<char, 8>* data) {
char buffer[data->size() * 2];
[... some code ...]
}
clearly the size of the buffer can be evaluated at compile time: data has a constexpr size of 8 elements, 8 * 2 = 16 bytes.
However, when compiling with -Wall, -pedantic and -std=c++11 I get the infamous error:
warning: variable length arrays are a C99 feature [-Wvla-extension]
which I believe makes sense: array::size() is constexpr, but it is still a method, and in the function above we still have to dereference a pointer, which is not constexpr.
If I try something like:
int test(std::array<char, 8>& data) {
char buffer[data.size() * 2];
[...]
}
gcc (tried version 5.2.0) seems happy: there is no warning.
But with clang++ (3.5.1) I still get a warning complaining about variable length arrays.
In my case, I can't easily change the signature of test, it has to take a pointer. So... a few questions:
What is the best / most standard way to get the size of a
std::arraypointer in constexpr context?Is the difference in behavior with pointers vs references expected? Which compiler is right about the warning,
gccorclang?
sizeisn't a static member function..template<typename T, std::size_t N> std::size_t arraysize(const std::array<T, N>& array) { return N; }which could be used above. Still... is this the right way? Seems contorted.nullptr; sure, it can't do anything useful there, but it also means thesizeisn't properly defined. Ifsizewere virtual, I could see it complaining with both pointers and references (because it might be a type derived fromstd::array<char, 8>that doesn't have the samesizeimplementation), but that's clearly not the case here.std::tuple_size<decltype(*arr)>::value?remove_reference_t, which makes it quite long.