473,322 Members | 1,405 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,322 software developers and data experts.

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>(A2());

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_twister or linear_congruential.
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_generator, and the class
uniform_random_number_generator_wrapper<T>. Note that
uniform_random_number_generator already exists, as a concept (table 5.1),
and the uniform_random_number_generator_wrapper<T> class is very simple,
just because all existing classes already are implemented according to the
uniform_random_number_generator 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_generator_wrapper class
template.

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

best regards,
Ares Lagae
Jul 22 '05 #1
3 1432
In article <10***************@seven.kulnet.kuleuven.ac.be>,
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>(A2());

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
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...
17
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...
43
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...
12
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...
5
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...
14
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...
5
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...
16
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.