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