Friday, March 28, 2008

Template Metaprogramming

We must have seen that C++ has incorporated template metaprogramming.
It also mentions that it gets calculated at compile time ( Wow! ).
This saves the execution time of a program which is great.
So lets see the standard example give in C++ Templates book by Vandevoorde/Josuttis.

template <int N>
class Pow3 {
public:
enum { result = 3 * Pow3<N-1>::result };
};

template <>
class Pow3<0> {
public:
enum { result = 1 };
};

Now when you write the program like :

int main() try {
cout << " 3 to power 3 is : " << Pow3<3>::result << endl;
return 0;
} catch(...) {
cout << "Uncaught exception..." << endl;
}

It prints : 27.
Conceptually enums are constants and constants are to be given value at compile time only.
Therefore, we can assume that Pow3<3>::result was having 27 as value.
But how will you prove that compiler did the actual calculation for you ???
If you are not sure check out the reply for this blog.

Thursday, March 6, 2008

Depth First Search without writing code

I am going to show how depth first search ( DFS ) without using any algorithm.
Actually I am making use of the algorithm which compilers already have implemented :-)
The virtual inheritance property is compiler makes sure that it follows the depth first search logic to instantiate the virtual base classes.
Here is the code and output:

/*
* =====================================================================================
*
* Filename: DepthFirstSearch.cpp
*
* Description: Without writing code DFS implementation
* A B C D
* \ / \ /
* E F
* \ /
* \ /
* G
* Version: 1.0
* Created: 03/06/2008 11:21:34 AM IST
* Revision: none
* Compiler: gcc
*
* Author: Siddhartha Singh :
* Company:
*
* =====================================================================================
*/

#include

using std::cout;
using std::endl;


class A {
public:
A() {
cout << "A()" << endl;
}
};
class B {
public:
B() {
cout << "B()" << endl;
}
};
class C {
public:
C() {
cout << "C()" << endl;
}
};
class D {
public:
D() {
cout << "D()" << endl;
}
};
class E : public virtual A, virtual public B {
public:
E() {
cout << "E()" << endl;
}
};
class F : public virtual C, virtual public D {
public:
F() {
cout << "F()" << endl;
}
};
class G : virtual public E, virtual public F {
public:
G() {
cout << "G()" << endl;
}
};

int main() try {
G g;
return 0;
} catch(...) {
cout << "Uncaught exception..." << endl;
}


--------------------------------------------------------
Output :
A()
B()
E()
C()
D()
F()
G()

So no need to write algo to find how DFS works ( Short cut to success ;-)

Wednesday, February 20, 2008

Diamond Ring formation is Not a problem But a solution

Many a times in C++ people ( mostly interviewers ) ask : What is Diamond Ring problem?
The reply should be : It is not a problem rather a solution.
Scenario : When there is a common base class inherited by a child from immediate base classes. It leads to ambiguity. For e.g., Consider this situiation
class B : public A {};
class C : public A {};
class D : public B, public C {} //this leads to two instances of A in D , one from B another from C

The diagram looks like :
A A
\ /
B C
\ /
D
This causes two instances of A to be in D. Which leads to ambiguity problem the moment you create an instance of D.

The way to resolve is A should be inherited to B and C virtually.
i.e
class B : virtual public A {}
class C : public virtual A {}
class D : public B, public C {}

When it is virtual inheritance, it differs from normal inheritance. Standard doesn't talk about how to implement it. But the behavior is when you create any child class' object in the hierarchy what is guaranteed is the A class' constructor gets called directly from the child class ( I will write another post as how to create final class in C++ using virtual inheritance ).
In case of virtual inheritance the diagram would look like (diamond ring). This solves the ambiguity problem by having reference to A in B and C and not the instance of A.

A
/ \
B C
\ /
D

Final wordings : Diamond ring is actually a solution and not a problem.