简体   繁体   中英

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++

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

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

Thanks martin

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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