Boost logo

Boost :

From: christopher diggins (cdiggins_at_[hidden])
Date: 2005-08-10 00:30:00


In the boost::any alternative implementation being developed by Pablo
Aguilar and myself (latest version at
http://www.codeproject.com/cpp/dynamic_typing.asp ) we have run into a
problem of alignment with no obvious answer.

We are trying to do the following:

struct any
{
  void* table;
  void* object;

  ...

  template<typename T>
  any(const T& x) {
    table = any_detail::get_table<T>::get();
    if (sizeof(T) <= sizeof(void*)) {
      if (new(&object) T(x) != &object) {
        throw runtime_error("can not align the data type ... pigs must be
flying");
      }
    }
  }
  else {
    object = new T(x);
  }
}

The question I have is whether in practice though are there are any
compilers which have stricter alignment requirements on small types (<=
sizeof(void*)) than a void pointer. In other words, is it possible with any
known compiler to actually throw the runtime error ?

Unfortunately there is another problem, I am not an expert in interepreting
the standard, but AFAICT placement new is guaranteed to return a pointer to
properly aligned memory (3.7.3.1 / 2) which I leverage to check that
alignment occured. Apparently not every STL implementation does in fact do
that (I believe at least dinkumware is guilty of that).

Thanks in advance for your help!

Christopher Diggins
http://www.cdiggins.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk