473,804 Members | 4,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compile time efficency / run time flexibility - best of both worlds

Suppose one is writing a library behavior the classes A1, A2 and A3, which
share some common behavior. Some applications will only use one of the
classes, while other applications will use all of them.

Because the classes share common behavior, the library designed has to
choose wether to introduce an abstract base class A, or not. Introducing an
ABC has the advantage of run time flexibility, but efficiency suffers
(virtual functions & inlining, ...).

To have the best of both worlds, you could try this:

class A1 // no inheritance
{
// no virtuals
f();
};

class A2
{
f();
};

class A3
{
f();
};

class A
{
virtual f();
};

template <class Ax>
class A_wrapper : public A
{
public:
A_wrapper(Ax& a) : a_(a) {}
f() { return a_.f(); }
private:
Ax a_;
}

Applications that use only one of the classes:

A2 a2;
a2.f();

Applications that need run time flexibility:

A * a = new A_wrapper<A2>(A 2());

What do you think of this library design technique ?
Note that this technique could be applied to e.g. the random number facility
that was proposed to the standard.
Most applications will choose one random number generator and use it, e.g.
mersenne_twiste r or linear_congruen tial.
However, some applications will (for example) want to study the effect the
random number generator has on their simulation. For these applications,
run time flexibility is important.
Run time flexibility can be accomplished by introducing the base class
uniform_random_ number_generato r, and the class
uniform_random_ number_generato r_wrapper<T>. Note that
uniform_random_ number_generato r already exists, as a concept (table 5.1),
and the uniform_random_ number_generato r_wrapper<T> class is very simple,
just because all existing classes already are implemented according to the
uniform_random_ number_generato r concept. And even if a class had a slightly
different interface, the problem could be solved using an explicit
specialization of the uniform_random_ number_generato r_wrapper class
template.

So again, what do you think of this library design technique ?

best regards,
Ares Lagae
Jul 22 '05 #1
3 1461
In article <10************ ***@seven.kulne t.kuleuven.ac.b e>,
Ares Lagae <ar********@cs. kuleuven.ac.be> wrote:
Suppose one is writing a library behavior the classes A1, A2 and A3, which
share some common behavior. Some applications will only use one of the
classes, while other applications will use all of them.

Because the classes share common behavior, the library designed has to
choose wether to introduce an abstract base class A, or not. Introducing an
ABC has the advantage of run time flexibility, but efficiency suffers
(virtual functions & inlining, ...).

To have the best of both worlds, you could try this:

class A1 // no inheritance
{
// no virtuals
f();
};

class A2
{
f();
};

class A3
{
f();
};

class A
{
virtual f();
};

template <class Ax>
class A_wrapper : public A
{
public:
A_wrapper(Ax& a) : a_(a) {}
f() { return a_.f(); }
private:
Ax a_;
}

Applications that use only one of the classes:

A2 a2;
a2.f();

Applications that need run time flexibility:

A * a = new A_wrapper<A2>(A 2());

What do you think of this library design technique ?


Unnecessary.

class A { virtual ~A() { } virtual void f() = 0; };

class A1 { void f(); };

To use only one of the classes:

A1 a1;
a1.f();

The above will pre-bind. The virtual mechanism will not be used.
Jul 22 '05 #2
Unnecessary.

class A { virtual ~A() { } virtual void f() = 0; };

class A1 { void f(); };

To use only one of the classes:

A1 a1;
a1.f();

The above will pre-bind. The virtual mechanism will not be used.


The why do new proposals (like the random number library) don't use abstract
base classes ? Are most C++ programmers still "afraid" of virtuals ?

best regards,
Ares Lagae

Jul 22 '05 #3
Ares Lagae <ar********@cs. kuleuven.ac.be> wrote:
Unnecessary.

class A { virtual ~A() { } virtual void f() = 0; };

class A1 { void f(); };

To use only one of the classes:

A1 a1;
a1.f();

The above will pre-bind. The virtual mechanism will not be used.


The why do new proposals (like the random number library) don't use abstract
base classes ? Are most C++ programmers still "afraid" of virtuals ?


Because they are out of vogue in C++ right now. Notice that very little
of the standard library use classes that contain virtual
member-functions. Off hand, I think that the only portion of the
standard that does use virtuals is the IO library, which pre-dates the
STL proposal (i.e. came into existence while classes with virtual
member-functions were more popular.)
Jul 22 '05 #4

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

Similar topics

1
1346
by: benevilent | last post by:
Hey, I am embedding Python in an application, and have an efficency concern about this. There is a Python method which is called from the main application (written in C) many times per second (I am using the PyObject_Call function). Unfortunately this seems to be a bottleneck, as when the performance is compared to another language frontend of this application, when there involves many transitions from the core application to the...
17
3134
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the absolute address of alpha to access to it. What confuses me is that when the variable is dynamically allocated, how does the compiler implement it? We know the address of the variable until run-time. During the compilation, how can we access to the...
43
2149
by: Mike MacSween | last post by:
Or something to do with Nulls in PK. Which is obviously wrong. OK, after a diversion of having to earn money its back to the free one. The orchestral management system. At the E-R level: An event (concert etc.), a player (of musical instruments), an ensemble (orchestra, band et al), an instrument.
12
3209
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the enums. These enums you might imagine would be like OrderPrice=27, OrderQuantity=50, OrderSide=62. There may be a lot of these. So normally what we would have to do is store these in hash table so hashtable=value could be set with the value....
5
1457
by: Michael | last post by:
i experience slower compile times with VC++ 2003 compared to VC+6.0. Anyone experiencing the same? Should that be expected? This ineed matters, when total compilation time is > 1h and you have to wait 10-50% longer...
14
4864
by: Mac via DotNetMonster.com | last post by:
Hi all, As some of you would know from my previous posts that I am rewriting our Unix based system into .Net with a MDI interface. Recently I have added a tab control to the top of the MDI parent form that will contain a tab page for each open MDI child form. It is only 24 pixels high so all you see is the tabstrip. Then I only have to add a bit of code to the "changed index" event of the tab control as well as the "on activate" event...
5
2231
by: Arno | last post by:
Hello, here is my problem: I have a complex data type, like a std::vector< std::string > and need some global constant values of this type. The type is also used non-constant throughout the program, so I do not want to use dumb types, but rather STL things. In the best of worlds, initialization of these constants would not happen at runtime, but at compile time, essentially generating a constant memory image of this type via...
16
5435
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will first be know at runtime. But is there some strict definitions that defines runtime code and compile time code that can be used in general?
0
9706
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
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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...
1
10319
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
9144
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...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
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
4297
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
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.