473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1974
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
0
by: Axter | last post by:
I'm currently working on the following policy base smart pointer: http://code.axter.com/smart_ptr.h Before working on the above code, I read the following links:...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
15
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.