1
votes

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
Any reason you're not using std::string?kmarsh
Are you saying that your code compiled and executed correcly in GCC 4.2.3?fpmurphy
It still compiles on that system, yes. And it seems to work.martin
@kmarsh: I have already replaced it with std::string (would have from the beginning); I inherited that piece of code, but I have to admit, I wouldn't have known it was illegal (I hardly ever use raw arrays these days); after the answers, it seems quite logical now that it should be forbidden. And I don't like to think about why it worked so far and whether it really did...martin

4 Answers

6
votes

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
votes

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.

2
votes

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.

-1
votes

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;