[英]C++11 variable size POD struct
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 ). 我正在C ++ 11中编写一个低级数据操作代码,并且我想在结构的末尾使用旧的C灵活数组功能( 请参阅此处的一些信息 )。
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 一旦我将GCC与参数结合使用
-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. 编辑:抱歉,我误认为仅C功能变成了C ++所没有的例外之一。 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 谢谢
As reference Incompatibilities Between ISO C and ISO C++ states in Flexible array members section: 作为参考,“ 灵活数组成员”部分中的ISO C和ISO C ++状态之间的不兼容性 :
C++ does not support flexible array members.
C ++不支持灵活的数组成员。
(This feature might be provided as an extension by some C++ compilers, but would probably be valid only for POD structure types.)
(此功能可能是某些C ++编译器的扩展功能,但可能仅对POD结构类型有效。)
gcc does support this as an extension as well as clang . gcc的确支持 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 . 显然,这也可以在Visual Studio中使用-如果您不使用
/Za
,可以实时查看它 ,我可以在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
. 我不认为这是沉默的警告可移植的方式,而是要像源内GCC部分禁用迂腐的警告应该工作
gcc
。
You can emulate it with c++ with a custom operator new 您可以使用带有自定义运算符new的c ++进行仿真
struct VariableCell {
VariableCell( std::uint32_t sz ) : cellSize(sz) {}
std::uint8_t* data() { return reinterpret_cast<std::uint8_t*>(this+1); }
static void* operator new(std::size_t sz, std::uint32_t dataSz) {
return ::operator new(sz+dataSz);
}
private:
std::uint32_t cellSize;
};
std::unique_ptr<VariableCell> CreateVariableCell( std::uint32_t size ) {
return std::unique_ptr<VariableCell>{ new (size) VariableCell{size} };
}
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). 您必须检查哪些标准,C ++ 11仍然不允许使用0长度数组(第8.3.4 / 1节)以及C99(第6.7.5.2/1节)。 It seems they're a C only feature, but I'd rather say an old C hack.
看来它们只是C的功能,但我宁愿说一个旧的C hack。 Are you sure you can't find a better solution for that?
您确定无法找到更好的解决方案吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.