Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

ATL's Advanced Use of Templates

The primary design goal for ATL is performance. To provide the maximum possible performance, ATL uses some advanced template idioms that you might not have seen elsewhere.

Many developers of lightweight components would like to avoid the overhead associated with virtual functions, for example. ATL declares abstract classes using the _declspec(novtable) compiler extension. This declaration prevents the compiler from emitting code to initialize the virtual function table for the abstract base class. When linked, the linker is able to perform further optimizations, reducing the size of the component.

ATL also avoids virtual functions whenever possible in favor of static linking. For an example of how ATL avoids the cost of virtual functions, consider the code shown in Listing 29.1, a typical example of how virtual functions are used in C++ programs.

Example 29.1. Using Virtual Functions in C++

#include <iostream>
using namespace std;
class CBase
{
public:
    void CallVirtualFunc()
    {
        VirtualFunc();
    }
    virtual void VirtualFunc()
    {
        cout << "CBase::VirtualFunc called" << endl;
    }
};

class CDerived: public CBase
{
    void VirtualFunc()
    {
        cout << "CDerived::VirtualFunc called" << endl;
    }
};

int main()
{

    CBase* pb = new CDerived;
    pb->CallVirtualFunc();

    return 0;
}

In Listing 29.1, the CDerived class implements a virtual function that is called via a pointer to CBase. Calling functions implemented in a derived class is a key abstraction in C++ and is very powerful. Many class libraries, including MFC, are built on this abstraction.

Unfortunately, the use of virtual functions in Listing 29.1 does not come without a price. In addition to the cost of building and initializing the virtual-function table, each function call made through the virtual-function table incurs a performance penalty of several instructions. In some applications, particularly in lightweight server-side components, this runtime cost can be a noticeable drag on the component's overall performance.

To avoid this performance cost, ATL uses static, compile-time binding instead of runtime binding whenever possible. Listing 29.2 illustrates this practice.

Example 29.2. Using Static Binding Instead of Virtual Functions

#include <iostream>
using namespace std;
template<typename T>
class _declspec(novtable) CBase
{
public:

    void CallVirtualFunc()
    {
        // Call the most derived version of the function directly.
        static_cast<T*>(this)->VirtualFunc();
    }
    virtual void VirtualFunc()
    {
        cout << "CBase::VirtualFunc called" << endl;
    }
};

class CDerived: public CBase<CDerived>
{
public:
    void VirtualFunc()
    {
        cout << "CDerived::VirtualFunc called" << endl;
    }
};

int main()
{

    CBase<CDerived>* pb = new CDerived;
    pb->CallVirtualFunc();

    return 0;
}

In Listing 29.2, the derived class type is passed as a template parameter. When the template is instantiated, code necessary to call the proper functions in the derived class is created, without the costs associated with virtual functions.

Share ThisShare This

Informit Network