简体   繁体   English

字符数组矢量的C ++ GCC 4.3.2错误

[英]C++ GCC 4.3.2 error on vector of char-array

It is similar in problem to this bug 问题与此漏洞类似

Question about storing array in a std::vector in C++ 关于在C ++中将数组存储在std :: vector中的问题

but for a different reason (see below). 但出于其他原因(请参见下文)。

For the following sample program 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 compiles cleanly. GCC 4.2.3可以干净地编译。 GCC 4.3.2 emits the following errors: 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(); }

which is called, I think, due to incorrect array-to-pointer-decay. 我认为这是由于不正确的数组到指针衰减导致的。

My question is: Is there anything in language standard preventing storage of arrays in std::vector? 我的问题是: 语言标准中是否有什么阻止数组存储在std :: vector中的? Or is it just a bug in that special GCC version? 还是仅仅是该特殊GCC版本中的错误?

I do believe that this should compile (ie 4.2.3 is correct). 我确实认为应该对此进行编译(即4.2.3是正确的)。

Thanks martin 谢谢马丁

Yes there is something in the standard stopping using arrays Using Draft C++98 Standard 是的,标准中有些东西停止使用数组使用Craft C ++ 98 Standard

Section 23 Containers 第23节集装箱

The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignabletypes. 存储在这些组件中的对象的类型必须满足CopyConstructible类型(20.1.3)的要求以及Assignabletypes的其他要求。

where components are various containers 其中组件是各种容器

20.1.3 includes the requirement that the type has to have a destructor. 20.1.3包括该类型必须具有析构函数的要求。

I think of it as a vector has to copy allocate and delete elements. 我认为它是一个必须复制分配和删除元素的向量。 How does C++ know to copy or delete a char[] ? C ++如何知道要复制或删除char []?

Nope, this is not allowed, just like it wasn't allowed in the question you linked to (and I don't see how the "reason is different"). 不,这是不允许的,就像您所链接的问题中不允许这样做一样(而且我看不出“原因有何不同”)。 It's not a "bug" in either of the two questions. 这不是两个问题中的一个“错误”。 Or at least, not a bug in the compiler. 或者至少不是编译器中的错误。 Just in your code. 只是在您的代码中。 ;) ;)

You can't store an array in a vector, because a vector requires its elements to be copy-constructible and assignable. 您不能将数组存储在向量中,因为向量要求其元素是可复制构造和可分配的。

The easiest solution to do this is: 最简单的解决方案是:

std::vector<boost::array<LENGTH> > ca_vector;

This way you don't have to bother with the array/pointer story. 这样,您不必理会数组/指针的故事。 Whether you really want this, is another question. 您是否真的想要这个,是另一个问题。

Please let me know mind of this code. 请让我知道此代码的介意。 You already know, vector is also some kind of an array and you dont have to give initial size. 您已经知道,向量也是某种数组,您不必给出初始大小。

1D vector  ->  std::vector<char*> ca_vector;
2D vector  ->  std::vector<char*,char*> ca_vector;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM