是std :: array< T,S>如果T是POD,则保证为POD? [英] Is std::array<T, S> guaranteed to be POD if T is POD?

查看:224
本文介绍了是std :: array< T,S>如果T是POD,则保证为POD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个C ++内存编辑库,对于读/写API,我使用类型traits(std :: is_pod,std :: is_same)和boost :: enable_if提供3个重载:

I'm currently writing a C++ memory editing library and for the read/write APIs I use type traits (std::is_pod, std::is_same) and boost::enable_if to provide 3 overloads:


  1. POD类型。例如MyMem.Read(SomeAddress);

  2. 字符串类型。例如MyMem.Read>(SomeAddress); (这实际上不读出C ++字符串,它读出一个C风格的字符串并将其转换为C ++字符串。)

  3. 向量类型。例如MyMem.Read>(SomeAddress,NumElem); (这不会实际读出一个向量,而是读出一个C风格的数组,并将其转换为向量。)

重载2和3只是在重载1的包装。(所以如果你正在读一个std :: vector或std :: basic_string,T不是POD,它会失败,因为它应该)。

Overloads 2 and 3 are simply 'wrappers' around overload 1. (So if you're reading a std::vector or std::basic_string and T is not POD it will fail, as it should.)

最近我想使用std :: array来读取和写入一堆数据,因为我知道我想在编译时读取和写入数据的大小(我正在写一个包装器PE文件格式)。

Recently I wanted to use std::array for a bunch of reads and writes because I knew the size of the data I wanted to read and write at compile time (I was writing a wrapper around the PE file format).

我写的代码使用std :: array,然后打算添加另一个重载用于检测和处理std :: array类型,但我不小心碰到编译,我的惊喜它的工作!

I wrote the code to use std::array, and then intended to add another overload for detection and handling of std::array types, but I accidentally hit compile and to my surprise it worked!

我目前使用MSVC 10,结果是std :: array如果T是POD然后std :: array是POD。 (这意味着我只能使用重载1和它的工作原理。)

I'm currently using MSVC 10 and it turns out that for std::array if T is POD then std::array is POD. (Which means I can just use overload 1 and it works.)

我的问题是这是由C ++标准保证还是留给实现。

My question is whether this is guaranteed by the C++ standard or left up to the implementation.

我知道我可以自己检查标准,但我不信任自己,因为我信任一些语言律师在这个网站,所以我认为这将是最好的得到第二意见。 ;)

I know I could check the standard myself, but I don't trust myself as much as I trust some of the language lawyers on this site, so I figured it would be best to get a 'second opinion'. ;)

感谢

这里的代码(它是一个只有头文件的lib):
http://code.google.com/p/hadesmem/source/browse/trunk/HadesMem-v2/Hades-Memory/Hades-Memory/MemoryMgr.h#86

P.S. Code available here (it's a header-only lib): http://code.google.com/p/hadesmem/source/browse/trunk/HadesMem-v2/Hades-Memory/Hades-Memory/MemoryMgr.h#86

推荐答案

§23.3.1:


数组是可以使用语法数组a< T,N>初始化的聚合(8.5.1) = {initializer-list};
其中initializer-list是最多N个元素的逗号分隔列表,其类型可以转换为T.

An array is an aggregate (8.5.1) that can be initialized with the syntax array a<T, N> = { initializer-list }; where initializer-list is a comma separated list of up to N elements whose types are convertible to T.

在C ++ 03中,POD根据聚合定义:一个类,其中每个子对象是本地的或聚合的是POD。因此,通过向后兼容性,C ++ 0x std :: array 是POD。

In C++03, POD was defined in terms of aggregate: a class where every subobject is native or an aggregate is POD. So, by backwards compatibility, a C++0x std::array is POD.

肛门,可以比较§9/ 5(定义琐碎类)9/6(定义标准布局)和9/9(将前面的要求合并为POD)的那些点与8.5.1 / 1的点

Or, to be anal, one can compare the bullet-points of §9/5 (defining trivial class) 9/6 (defining standard-layout) and 9/9 (combining preceding requirements into POD) with those of 8.5.1/1, which defines aggregates.

8.5.1:


聚合是数组或没有用户提供的构造函数(12.1),没有用于非静态数据成员(9.2),没有私有或受保护的非静态数据成员(第11条)的支持或平等初始化器,没有基类(第10条),没有虚函数(10.3)。

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal- initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

,只要其元素类型也是POD,并且实现未声明 operator = move

Indeed the requirements in Clause 9 cover array as long as its element type is also POD and the implementation does not declare operator= or move inside array in addition to the specifications.

真的肛门, 17.5.2.2说

To be really anal, 17.5.2.2 says



  1. 为了说明的目的,第18至30条和附录D没有描述复制/移动构造函数,赋值运算符或具有与默认情况下可以生成的语义相同(12.1,12.4,12.8)的(非虚拟)析构函数。

  2. 该实现为这样的成员函数信号提供显式定义,或为可以默认生成的虚拟析构函数提供显式定义。


模板类数组的伪代码中的注释是


//对于聚合类型没有显式构造/复制/销毁

构造/复制/销毁包括 operator = (赋值)或 move ?它可能应该,但我不认为,通过最严格的阅读,它是。

Does construct/copy/destroy include operator= (assignment) or move? It probably should, but I don't think, by the strictest reading, it does.

请注意,这不仅影响POD,而且微不足道的可复制性正如Johannes提到的。

Note that this "affects" not only POD-ness, but also trivial copyability as Johannes mentions.

这篇关于是std :: array&lt; T,S&gt;如果T是POD,则保证为POD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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