C++ GCC 4.3.2 error on vector of char-array

◇◆丶佛笑我妖孽 提交于 2019-12-11 04:34:57

问题


It is similar in problem to this bug

Question about storing array in a std::vector in C++

but for a different reason (see below).

For the following sample program in 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.3.2 emits the following errors:

/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? Or is it just a bug in that special GCC version?

I do believe that this should compile (i.e. 4.2.3 is correct).

Thanks martin


回答1:


Yes there is something in the standard stopping using arrays Using Draft C++98 Standard

Section 23 Containers

The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignabletypes.

where components are various containers

20.1.3 includes the requirement that the type has to have a destructor.

I think of it as a vector has to copy allocate and delete elements. How does C++ know to copy or delete a char[] ?




回答2:


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.




回答3:


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.




回答4:


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;


来源:https://stackoverflow.com/questions/1559786/c-gcc-4-3-2-error-on-vector-of-char-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!