和这个bug问题类似
Question about storing array in a std::vector in C++
但出于不同的原因(见下文)。
对于以下 C++ 示例程序:
#include <vector>
int main(int c_, char ** v_)
{
const int LENGTH = 100;
std::vector<char[LENGTH]> ca_vector;
return 0;
}
GCC 4.2.3 编译干净。 GCC 4.3.2 发出以下错误:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_construct.h: In function ‘void std::_Destroy(_Tp*) [with _Tp = char [100]]’: /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_construct.h:103: instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = char (*)[100]]’ /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_construct.h:128: instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator&) [with _ForwardIterator = char (*)[100], _Tp = char [100]]’ /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_vector.h:300: instantiated from ‘std::vector::~vector() [with _Tp = char [100], _Alloc = std::allocator]’ test.cpp:7: instantiated from here /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_construct.h:88: error: request for member ‘~char [100]’ in ‘* __pointer’, which is of non-class type ‘char [100]’
The reason apparently is this bit in
include/g++-v4/bits/stl_construct.h
template
inline void
_Destroy(_Tp* __pointer)
{ __pointer->~_Tp(); }
我认为这是由于不正确的数组到指针衰减而被调用的。
我的问题是: 语言标准中是否有任何内容阻止在 std::vector 中存储数组? 或者它只是那个特殊 GCC 版本中的错误?
我相信这应该可以编译(即 4.2.3 是正确的)。
谢谢 马丁
最佳答案
是的,标准中有一些东西停止使用数组使用Draft C++98 Standard
第 23 节容器
这些组件中存储的对象类型必须满足CopyConstructible的要求 类型(20.1.3),以及 Assignabletypes 的附加要求。
其中组件是各种容器
20.1.3 包括类型必须具有析构函数的要求。
我认为它是一个 vector ,必须复制分配和删除元素。 C++ 如何知道复制或删除 char[] ?
关于字符数组 vector 上的 C++ GCC 4.3.2 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1559786/