C ++ 11可变大小POD结构 [英] C++11 variable size POD struct

查看:146
本文介绍了C ++ 11可变大小POD结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++ 11中编写一个低空数据操作代码,我想在结构的末尾使用一个已知的灵活数组的C特性(查看这里的一些信息)。

  struct variableCell 
{
/ **
* @brief以下数据的字节大小。
* /
std :: uint32_t cellSize;

/ **
* @brief数据存储在叠加层中。
* /
std :: uint8_t cellData [];
};

一旦我将GCC与参数一起使用

  -Wall -pedantic -std = c ++ 11 

我得到这个waring

  xxx.h:xx:warning:ISO C ++禁止零大小数组'variableCell'[-Wpedantic] 

这是一个完全正确的功能。请不要告诉我,我的方法是错误的 - 它是低级数据操作的正确方法。



为什么标准改变了这一点,



编辑
b我的道歉,我错了只C的功能,这变成了C ++不包括的例外之一。我会考虑使用不同的方法。但为了我的好奇心,哪些编译器允许这是一个非标准的扩展,以及如何让他们接受它,没有警告?



感谢

解决方案

作为参考

部分中:

$ c

不支持灵活的数组成员。



(此功能可能由某些C ++编译器作为扩展提供,但可能仅对POD结构类型有效。)


gcc不支持此扩展以及 ang 。显然,如果您不使用 / Za,这也适用于 Visual Studio - 看到它现场 ,我可以找到除此之外的任何文档 post by Stephan T. Lavavej



我不认为有一种可移植的方式沉默警告,但类似于在gcc中部分禁用教学警告source 应该适用于 gcc


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

解决方案

As reference Incompatibilities Between ISO C and ISO C++ states in Flexible array members section:

C++ does not support flexible array members.

(This feature might be provided as an extension by some C++ compilers, but would probably be valid only for POD structure types.)

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.

这篇关于C ++ 11可变大小POD结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆