
----- Original Message ----- From: "christopher diggins" <cdiggins@videotron.ca> To: "Boost mailing list" <boost@lists.boost.org> Sent: Wednesday, August 10, 2005 1:30 AM Subject: [boost] alignment problem in proposed any alternative
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 ?
Now a moot point, see below.
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).
Apparently I am wrong about this! 18.4.1.3 provides an exception. (I wonder why I missed the exception 15 chapters later?) Anyway it turns out we can guarantee alignment by simply using a char buffer. According to 3.9/2 the following automatically has no alignment problems: template<int buffer_size> struct any { char buffer[buffer_size]; ... template<typename T> any(const T& x) { if (boost::is_pod<T>::value && (sizeof(T) <= buffer_size)) { memcpy(buffer, &T, N); } else { // new and stuff } } } Christopher Diggins http://www.cdiggins.com