472,353 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 1910
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...
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...
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 (:...
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...
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...
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...
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...
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...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.