473,651 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me Enum>
struct EncodePrefix
{
template<Enum e>
struct Apply
{
struct Type
{
const unsigned char Value;
Type() : Value((unsigned char)e) {}
};
};

template<>
struct Apply<(Enum)ENU M_NONE>
{
//typedef Apply Type;
struct Type {};
};
};
template<EnumRe pLockPrefix RepLockPrefix, EnumSegPrefix SegPrefix
,EnumOperandSiz ePrefix OperandSizePref ix
,EnumAddressSiz ePrefix AddressSizePref ix
,EnumSimdPrefix SimdPrefix>
struct EncodePrefixes
: EncodePrefix<En umRepLockPrefix >::Apply<RepLoc kPrefix>::Type
, EncodePrefix<En umSegPrefix>::A pply<SegPrefix> ::Type
, EncodePrefix<En umOperandSizePr efix>
::Apply<Operand SizePrefix>::Ty pe
, EncodePrefix<En umAddressSizePr efix>
::Apply<Address SizePrefix>::Ty pe
, EncodePrefix<En umSimdPrefix>:: Apply<SimdPrefi x>::Type
{
};

So when I define EncodePrefixes like this:

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

I expect:
std::assert(siz eof(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 1994
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<unsign ed char Prefix_, typename Next = void>
struct EncodePrefix
{
struct Type : Next::Type
{ const unsigned char Prefix; Type() : Prefix(Prefix_) {} };
};

template<typena me Next>
struct EncodePrefix<EN UM_NONE, Next>
{
struct Type : Next::Type { };
};

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

template<>
struct EncodePrefix<EN UM_NONE, void>
{
struct Type { };
};

template<EnumRe pLockPrefix RepLockPrefix
,EnumSegPrefix SegPrefix
,EnumOperandSiz ePrefix OperandSizePref ix
,EnumAddressSiz ePrefix AddressSizePref ix
,EnumSimdPrefix SimdPrefix>
struct EncodePrefixes
: EncodePrefix<Re pLockPrefix,
EncodePrefix<Se gPrefix,
EncodePrefix<Op erandSizePrefix ,
EncodePrefix<Ad dressSizePrefix ,
EncodePrefix<Si mdPrefix> > > > >
{
};
Mar 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3001
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 out that you can inherit using the virtual keyword: class A { } class B : public *virtual* A
8
2851
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 class is executed. It means that three derived classes are pointed to a separated copy of base class. I do not allow second and third copy of base class to be created, but it must remain only first copy of base class. It allows three derived...
6
2827
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
4335
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 inherited base member's access level in the derived class: its access level in the base class and the type of inheritance (public, protected, or private). After this determination is made, the following possibilities exist for manually changing the...
0
1576
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: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1681.pdf http://www.cuj.com/documents/s=8890/cujexp0310alexandr/alexandr.htm I notice the above links only talk about using policy base logic via direct inheritance and multiple inheritance.
3
2236
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
28346
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 an error saying you are not allowed Multiple base classes. /// <summary>
47
4004
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 support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
18
2448
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 that one can implement a very basic form of subclassing in C (the gurus certainly already know this, but it was news to me). What i've done (shown below) seems to work all fine and well, and does exactly what i'd expect, but i'm asking about
0
8361
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.