468,119 Members | 1,902 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Empty base member optimisation with multiple inheritance

Hi,

I have a bit of code where I am relying on empty base member
optimisation. The bit of code is below:

template<typename Enum>
struct EncodePrefix
{
template<Enum e>
struct Apply
{
struct Type
{
const unsigned char Value;
Type() : Value((unsigned char)e) {}
};
};

template<>
struct Apply<(Enum)ENUM_NONE>
{
//typedef Apply Type;
struct Type {};
};
};
template<EnumRepLockPrefix RepLockPrefix, EnumSegPrefix SegPrefix
,EnumOperandSizePrefix OperandSizePrefix
,EnumAddressSizePrefix AddressSizePrefix
,EnumSimdPrefix SimdPrefix>
struct EncodePrefixes
: EncodePrefix<EnumRepLockPrefix>::Apply<RepLockPref ix>::Type
, EncodePrefix<EnumSegPrefix>::Apply<SegPrefix>::Typ e
, EncodePrefix<EnumOperandSizePrefix>
::Apply<OperandSizePrefix>::Type
, EncodePrefix<EnumAddressSizePrefix>
::Apply<AddressSizePrefix>::Type
, EncodePrefix<EnumSimdPrefix>::Apply<SimdPrefix>::T ype
{
};

So when I define EncodePrefixes like this:

EncodePrefixes<NONE,NONE,NONE,NONE,NONE> ep;

I expect:
std::assert(sizeof(ep) == 1);

Unfortunately this is not the case because the msvc doesn't do empty
base member optimisation for multiple inheritance, in-case you are
wondering, sizeof(ep) == 4.

Does anyone have a work-around for which doesn't involve explicit
specialisation of the EncodePrefix class 5*2 times.

---
David
Mar 6 '06 #1
1 1696
David Welch wrote:
Hi,

I have a bit of code where I am relying on empty base member
optimisation.


Ahhhhhh, I solved the problem myself ;-)

template<unsigned char Prefix_, typename Next = void>
struct EncodePrefix
{
struct Type : Next::Type
{ const unsigned char Prefix; Type() : Prefix(Prefix_) {} };
};

template<typename Next>
struct EncodePrefix<ENUM_NONE, Next>
{
struct Type : Next::Type { };
};

template<unsigned char Prefix_>
struct EncodePrefix<Prefix_, void>
{
struct Type
{ const unsigned char Prefix; Type() : Prefix(Prefix_) {} };
};

template<>
struct EncodePrefix<ENUM_NONE, void>
{
struct Type { };
};

template<EnumRepLockPrefix RepLockPrefix
,EnumSegPrefix SegPrefix
,EnumOperandSizePrefix OperandSizePrefix
,EnumAddressSizePrefix AddressSizePrefix
,EnumSimdPrefix SimdPrefix>
struct EncodePrefixes
: EncodePrefix<RepLockPrefix,
EncodePrefix<SegPrefix,
EncodePrefix<OperandSizePrefix,
EncodePrefix<AddressSizePrefix,
EncodePrefix<SimdPrefix> > > > >
{
};
Mar 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Michael Winter | last post: by
8 posts views Thread by Bryan Parkoff | last post: by
6 posts views Thread by Paul | last post: by
3 posts views Thread by hurcan solter | last post: by
15 posts views Thread by iKiLL | last post: by
47 posts views Thread by Larry Smith | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.