I am writing a low-lewel data manipulation code in C++ 11 and I want to use an old-known C feature of flexible arrays at the end of the struct (see some info here).
struct variableCell
{
/**
* @brief Size in bytes of following data.
*/
std::uint32_t cellSize;
/**
* @brief Data stored in overlay.
*/
std::uint8_t cellData[];
};
Once I use GCC with the paramaters
-Wall -pedantic -std=c++11
I get this waring
xxx.h:xx: warning: ISO C++ forbids zero-size array 'variableCell' [-Wpedantic]
This used to be a perfectly correct feature. Please, do not tell me that my approach is wrong - it is and always has been correct way for low-level data manipulation.
Why the standard changed this and how to disable just this one particular warning?
Thanks
Edit: My apologies, I mistaken the C only feature which turned to be one of the exceptions which does not the C++ include. I will consider using a different approach. But just for my curiosity, which compilers allows this as a non-standard extension and how to make them accept it without the warning?
Thanks
You can emulate it with c++ with a custom operator new
You have to check which standards, C++11 still disallows 0-length arrays (§8.3.4/1), and also C99 (§6.7.5.2/1). It seems they're a C only feature, but I'd rather say an old C hack. Are you sure you can't find a better solution for that?
As reference Incompatibilities Between ISO C and ISO C++ states in Flexible array members section:
gcc does support this as an extension as well as clang. Apparently this also works in Visual Studio - see it live if you don't use
/Za
which I can find any documentation on besides this post by Stephan T. Lavavej.I don't think there is a portable way to silence the warning, but something like Partially disable pedantic warnings in gcc within source should work for
gcc
.