Template Mania

Environment: VC6 SP5, NT4 SP3, Windows 98, Windows 2000

There is an interesting article concerning template-based technique on www.cuj.com [1]. A simple struct is introduced there that helps to implement dummy arguments converting values to distinquishable types:

template<int v>
struct Int2Type
   enum { value = v };
The main article idea is generally described in terms of classes, but there is a reason why it can be especially useful for template functions with VC. There is a bug Q240871 in the Microsoft Visual C++ 6.0 compiler, its description from the MSDN is given below:


If all the template parameters are not used in function arguments or return type of a template function, the template functions are not overloaded correctly.


The bug is the result of the way the compiler decorates template function names. Name Decoration uses the arguments and return type and doesn't use the explicitly specified template argument type. Therefore, all three template function instantiations receive the same decorated name<...>


Use dummy arguments to the function.

There is another interesting technique outlined in [2]. Templates do not deal with objects hierarchy, but dummy arguments combined with ellipses make it possible to specialize templates for classes and their subclasses. Let us implement a pointer validity checking utility based on these principles as an example.

MFC has two validating macros: ASSERT_VALID for CObject-based classes and ASSERT_POINTER for others. We introduce a single template function that supersedes both of them. The whole stuff being workable, some details will be omitted for simplicity. ASSERT_VALID macro invokes AfxAssertValidObject(). Among other things, it checks the object virtual table pointer. To do so, we need to find out if the class has one first. The next accessory struct can help us:

template<class T> struct HasVirtualTable
   class X : public T
      virtual void dummy();
   enum { has_table = sizeof(X) == sizeof(T) };
Then we introduce an AssertVTable template function for the "TRUE" and its void specialization for the "FALSE" (0) Int2Type template parameter:
template<typename T> inline void AssertVTable(const void* pData, T)
  ASSERT(AfxIsValidAddress(*(void**)pData, sizeof(void*), FALSE));

inline void AssertVTable(const void*, Int2Type<0>) {}
Here goes the common validating routine with an ellipse at the end of the arguments list:
template<class T> inline void AssertValidPointer(const T* pData, ...)
  ASSERT(AfxIsValidAddress(pData, sizeof(T)));
The next validating routine substitutes the previous one for CObject subclasses:
template<class T> inline void AssertValidPointer(const T* pOb,
                                                      const CObject*)
  ASSERT(AfxIsValidAddress(pOb, sizeof(T)));
  ASSERT(AfxIsValidAddress(*(void**)pOb, sizeof(void*), FALSE));
We can add customized validating routine for any class hierarchy branch in the same way.

Here are the top level functions to be used in the application:

template<class T> inline void CHECK_ADDRESS(const T* pData)
  AssertValidPointer(pData, pData);
template<class T> inline void CHECK_NULL_OR_ADDRESS(const T* pData)
  if(NULL != pData)
     AssertValidPointer(pData, pData);
We put stuff listed above into the #if/#else/#endif brackets to disable the functionality in the release build:
#ifdef _DEBUG
#else //_DEBUG
#define CHECK_ADDRESS(pData)          ((void)0)
#define CHECK_NULL_OR_ADDRESS(pData)  ((void)0)
#endif //!_DEBUG
Now we can use the CHECK_ADDRESS() and CHECK_NULL_OR_ADDRESS() functions.

At the end of the article I would dare to propose an exotic way to simulate enums using templates. It was written for fun only, but seems to be interesting and can actually help when there is no other way to keep enum items together:

#include <iostream>

#define ENUM(val) \
template<> struct cnt<__LINE__> { enum {v = cnt<__LINE__-1>::v+1}; }; \
enum { val = cnt<__LINE__>::v };	\
template<> struct flg<val> { enum {v = 0}; };

namespace test
  template<int i> struct flg { enum {v = -1}; };
  template<int i> struct cnt { enum {v = cnt<i-1>::v }; };
  template<> struct cnt<__LINE__> { enum {v = -1 }; };


// Some stuff here


// Some stuff here


  template<int i> struct bnd 
        u = bnd<(i+1) | flg<i+1>::v>::v,
        v = (-1 == u)? i : u
  template<> struct bnd<-1> { enum { v = -1 }; };
  enum { upper_bound = (bnd<0>::v | flg<0>::v)+1 };

int main()
  cout << test::a << ',' 
       << test::b << ',' 
       << test::c << ',' 
       << test::upper_bound << "\n";
  return 0;


[1] Andrei Alexandrescu "Generic Programming: Mappings between Types and Values"

[2] "Template Overloading For Base Class Pointers", by Don Wong


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds