Re: alignment issues
On Sep 24, 8:13 am, Stephen Horne <sh006d3...@blueyonder.co.uk> wrote:
I understand that the next C++ standard will have features to
handle the alignment of data types. This is good, but a bit
late for me!
I've been using some template trickery to handle alignment
issues for some time. What I usually want is a type that takes
the same amount of space and has the same alignment as some
other type, but which doesn't have constructors, destructors
etc. The idea is that initialisation and cleanup can be done
with casts, placement new and explicit destructor calls, but
that this memory-only type can live in a union or whatever.
To get the alignment of an existing type, I use something
like...
template<typename T>
class CAlign_Of
{
private:
struct CDummy
{
char m_Char;
T m_T;
};
public:
enum { Align = ((size_t) &(((CDummy*) 0)->m_T)) };
};
Which I figure should be portable to any platform where char
is a single byte (though I don't think even that is
guaranteed).
It is. By definition, char is a byte, and all other types
consist of an integral number of bytes.
To create the replacement type, however, is a bit more of a
problem. To create a type the right size, you just use a
template struct containing and array of chars, but getting the
alignment right is the problem.
So...
1. Is there a good portable solution now?
2. What will the standard C++ solution be?
3. How consistent are compilers in their non-standard alignment
handling right now? - e.g. does GCC c++ support an __alignof
extension similar to that on MS VC++?
Technically, I don't think that there is a solution 100%
guaranteed by the standard. Practically, I use the following:
namespace GlobalPrivate {
template< typename T, bool isSmaller >
struct AlignTypeDetail ;
template< typename T >
struct AlignTypeDetail< T, false >
{
typedef T type ;
} ;
template< typename T >
struct AlignTypeDetail< T, true >
{
typedef char type ;
} ;
template< typename T, typename U >
struct AlignType
{
typedef typename AlignTypeDetail< U, (sizeof( T ) <
sizeof( U )) >::type
type ;
} ;
}
template< typename T >
union MaxAlignFor
{
typename GlobalPrivate::AlignType< T, char >::type c ;
typename GlobalPrivate::AlignType< T, short >::type s ;
typename GlobalPrivate::AlignType< T, int >::type i ;
typename GlobalPrivate::AlignType< T, long >::type l ;
typename GlobalPrivate::AlignType< T, long long >::type ll ;
typename GlobalPrivate::AlignType< T, float >::type f ;
typename GlobalPrivate::AlignType< T, double >::type d ;
typename GlobalPrivate::AlignType< T, long double >::type ld ;
typename GlobalPrivate::AlignType< T, void* >::type pc ;
typename GlobalPrivate::AlignType< T, MaxAlign* >::type ps ;
typename GlobalPrivate::AlignType< T, void (*)() >::type pf ;
} ;
and then declare a union:
union
{
MaxAlignFor< T > dummyForAlignment ;
unsigned char data[ sizeof( T ) ] ;
} ;
This supposes that 1) the required alignment will not be more
than the alignment of one of the types in my MaxAlignFor union,
and 2) it will not be more than the size of the type. The
latter is more or less guaranteed by the standard (albeit very
indirectly); the former seems safe for now, and if it does cause
problems in the future, it shouldn't be any real problem to add
another type to the MaxAlignFor union. (All such types must be
POD, but I can't imagine that being a problem.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34