#include "boost/type_traits/alignment_traits.hpp" #include "boost/type_traits/aligned_storage.hpp" #include #include #include using namespace std ; using namespace boost ; struct X { X ( char x_, long double y_ ) : x(x_), y(y_) {} friend ostream& operator << ( ostream& os, X const& obj ) { return os << "[x:" << obj.x << ",y:" << obj.y << "]" ; } char x ; long double y ; } ; struct Z { Z ( float z_ ) { fill(z,z+7,z_); } friend ostream& operator << ( ostream& os, Z const& obj ) { os << "[" ; copy(obj.z,obj.z+7,ostream_iterator(cout)); os << "]" << endl ; return os ; } float z [7]; } ; template void test( T const& x, T const& y) { typedef alignment_alias::type alias ; cout << typeid(T).name() << " aligns as " << typeid(alias).name() << endl ; typedef aligned_storage tuple_storage_type ; tuple_storage_type tuple_storage ; cout << "sizeof( T[2] ) = " << sizeof(T)*2 << endl ; cout << "sizeof( tuple_storage_type ) = " << sizeof(tuple_storage_type) << endl ; T* tuple = tuple_storage.address(); tuple[0] = x ; tuple[1] = y ; cout << "x=" << x << " tuple[0]=" << tuple[0] << endl ; cout << "y=" << y << " tuple[1]=" << tuple[1] << endl ; cout << endl ; } int main() { int i0 = 123 ; int i1 = 456 ; test(i0,i1); double d0 = 3.14 ; double d1 = 6.28 ; test(d0,d1); X x0('a',6.02e23); X x1('e',M_E); test(x0,x1); Z z0(M_LOG2E); Z z1(M_LN2); test(z0,z1); }