Gianni Mariani wrote:
James Kanze wrote:
On May 17, 11:34 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
On May 17, 11:02 am, "Chris Thomasson" <cris...@comcas t.netwrote:
[...]
Now I need to figure out how to get the correct alignment
of any C++ type a user can come up with...
one way:
template <typename T>
struct alignof_static
{
struct Helper1 { T v; };
struct Helper2 { char c; Helper1 v; };
static const unsigned value = sizeof(Helper2)-sizeof(Helper1) ;
Just curious, but why Helper1? I would have expected:
struct Helper { char c; T v ; } ;
static const size_t value = sizeof( Helper ) - sizeof( T ) ;
Think about what the expression above does when T = char &.
Bingo. That's what I hadn't thought of.
I'm not sure that it's all that useful to ask the alignment of
something that the standard says might not occupy memory:-). On
the other hand, for most people, the case is probably more
likely to occur in practice that is the case of a word addressed
machine (unless you're a mainframes programmer, and your company
buys from Unisys). A good library should probably handle both
(i.e. word addressed machines, and the alignment of references)
correctly.
(The simplest solution would probably be to use both techniques,
forcing an error if the results weren't equal. That would
probably handle 99% of the cases, and would cause a compile time
error for the cases it didn't handle. Otherwise, I think that
it should be possible to use some metaprogramming tricks to use
one technique for references, and the other otherwise.)
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34