Boost logo

Boost :

From: scleary_at_[hidden]
Date: 2000-01-19 09:59:25


> I think that it will work provided that the platform alignment is a power
> of 2 and less than sizeof(our_test_class), the problem with the old one
was
> that if alignment was 8 bytes and the test class added a 4-byte int to the
> 8-byte empty structure, then we weren't filling out the empty struct
> completely.

Actually, we're dealing with structure padding, not alignment, though the
two are related.

OK. What do you think of the following -- the intent is to determine how
the compiler is padding its structures:

namespace details {
template <typename T, bool b>
struct empty_helper{ static const bool value = false; };

struct empty_known { };

template <typename T>
struct empty_helper_t1 : public T { char i[sizeof(empty_known)]; };
struct empty_helper_t2 { char i[sizeof(empty_known)]; };

template <typename T>
struct empty_helper<T, true>
{
   static const bool value = (sizeof(empty_helper_t1<T>) ==
sizeof(empty_helper_t2));
};
} // namespace details

//*? is type T an empty composite type (allows cv-qual)
template <typename T> struct is_empty
{ static const bool value = details::empty_helper<T,
is_class<T>::value>::value
      || BOOST_IS_EMPTY(typename remove_cv<T>::type); };

We're still making some assumptions:
  The compiler pads all classes using the same algorithm, meaning: it makes
each class at least a certain number of bytes (the same number for all
classes), and does not pad classes that don't need it. This second
statement can be tested by (sizeof(empty_known) == sizeof(empty_helper_t2)).
The first statement cannot be reliably tested, but is reasonable to assume.

Thoughts? In particular, are there any assumptions I've overlooked? They
should be well-documented.

        -Steve


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