-
#
7)
4#
8
-
(!)
4#
# /)!-
,
") , 7)
5(8
58
%10 -#
(!)
4#
#( -#
%10 -#
,
#
1
") ,.#
)
#
!6
4#
-#
377)
58
)&#
") , 7)
#
'
%
#
)
#
'
%
#
76
#
)
58
58
Foranytypelist
-
#
,
4#
-
#
17!)
returnsatcompiletimethemaximumsizeof
alltypescontainedin
-
#
.Using
4#
,
#7"
storesitsdatainthefollowingmanner(alignment
issueswillbedealtwithinthenextsection):
-
#
#"
,
") ,#
4#
-
#
37!) 5(8
)"#7$"%( &)'' 6
#
18
;;;
58
In addition,
(7#"
must store the discriminator, a tag that helps identifying the correct type
stored in theraw buffer. A simpleapproach would be to useanintegral tag; for flexibility and speed
reasons,
(7#"
storesapointertoastaticarray ofpointerstofunctions–anemulatedvirtualtable.
Section5describesthetypetagimplementation.
4.ComputingAlignment
NoapproachtocomputingalignmentinC++isentirelyportable,becausethelanguagelacksthe
appropriate primitives (such as the
66#$"'!66
keyword implemented by some compilers as an
extension). However, with some effort and a number of reasonable assumptions, alignment can be
computedaccuratelyonalargesetofplatforms.
The alignment problem to solve is: given a typelist, return a POD (plain old data) type that
guaranteesproperalignmentforanytypeinthetypelist.
For computing alignment, let’s first consider a typelist
-
'
#$""
that contains
typesofvariousalignments:
a) Allprimitivetypes
b) Pointerstoallprimitivetypes
c) Apointertofunction
d) Apointertomembervariable
e) Apointertomemberfunction
f) Aclasshavingavirtualfunction
Foreachtypementionedin(a)–(e),addaPODstructurecontainingexactlyonememberofthat
type.Thisadditionisnecessarybecausesomecompilersalignstructuresdifferentlythanprimitivetypes,
evenwhentheyarestructurallyequivalenttoprimitivetypes.
The
#$"")
algorithmthatcomputesalignmentforthetypesinatypelist
-
#
isthefollowing:
• Assign
-
'
#7$""
to
-
.
• Computethemaximum size of all typescontainedin
-
#
using the
4#
class
template described in Section 3 above. Store the result in the compile-time constant
4#
.
• Removefrom
-
alltypesthathaveasizegreaterthan
4#
.