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

2019-08-02 03:35发布

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

4条回答
在下西门庆
2楼-- · 2019-08-02 04:13

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.

查看更多
Animai°情兽
3楼-- · 2019-08-02 04:17

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[] ?

查看更多
一夜七次
4楼-- · 2019-08-02 04:19

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;
查看更多
\"骚年 ilove
5楼-- · 2019-08-02 04:22

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.

查看更多
登录 后发表回答