bivect: workaround flexible arrays not being standard C++

This is a -pedantic warning from gcc.

* src/misc/bitvect.cc, src/misc/bitvect.hh (storage_): Remove.
(storage): New method to access past the end of the struct.
This commit is contained in:
Alexandre Duret-Lutz 2014-08-19 22:50:08 +02:00
parent 52ce449bbc
commit 14570f62d0
2 changed files with 14 additions and 4 deletions

View file

@ -160,7 +160,7 @@ namespace spot
bitvect_array* bva = new(mem) bitvect_array(vectcount, bvsize); bitvect_array* bva = new(mem) bitvect_array(vectcount, bvsize);
// Initialize all the bitvect instances. // Initialize all the bitvect instances.
for (size_t i = 0; i < vectcount; ++i) for (size_t i = 0; i < vectcount; ++i)
new(bva->storage_ + i * bvsize) bitvect(bitcount, n); new(bva->storage() + i * bvsize) bitvect(bitcount, n);
return bva; return bva;
} }

View file

@ -456,6 +456,17 @@ namespace spot
SPOT_LOCAL bitvect_array(const bitvect_array&) SPOT_DELETED; SPOT_LOCAL bitvect_array(const bitvect_array&) SPOT_DELETED;
SPOT_LOCAL void operator=(const bitvect_array&) SPOT_DELETED; SPOT_LOCAL void operator=(const bitvect_array&) SPOT_DELETED;
// Extra storage has been allocated at the end of the struct.
char* storage()
{
return reinterpret_cast<char*>(this) + sizeof(*this);
}
const char* storage() const
{
return reinterpret_cast<const char*>(this) + sizeof(*this);
}
public: public:
~bitvect_array() ~bitvect_array()
{ {
@ -473,14 +484,14 @@ namespace spot
bitvect& at(const size_t index) bitvect& at(const size_t index)
{ {
assert(index < size_); assert(index < size_);
return *reinterpret_cast<bitvect*>(storage_ + index * bvsize_); return *reinterpret_cast<bitvect*>(storage() + index * bvsize_);
} }
/// Return the bit-vector at \a index. /// Return the bit-vector at \a index.
const bitvect& at(const size_t index) const const bitvect& at(const size_t index) const
{ {
assert(index < size_); assert(index < size_);
return *reinterpret_cast<const bitvect*>(storage_ + index * bvsize_); return *reinterpret_cast<const bitvect*>(storage() + index * bvsize_);
} }
friend SPOT_API bitvect_array* friend SPOT_API bitvect_array*
@ -495,7 +506,6 @@ namespace spot
private: private:
size_t size_; size_t size_;
size_t bvsize_; size_t bvsize_;
char storage_[0];
}; };
/// @} /// @}