TN004: C++ Template Tool

This note describes MFC template-class issues and the MFC TEMPLDEF template expansion tool.

The Problem

MFC uses template classes to implement the standard collection classes. Template classes are classes that can be expanded based on a type argument specified by the user. For the MFC standard collection classes, the classes in AFXCOLL.H and their implementations are already pre-expanded for you. To create your own collection classes for your own type, you can use AFXTEMPL.H which contains array, list, and map class templates using standard C++ template syntax. The TEMPLDEF tool is provided only for backward compatibility (in case you were using it to generate your own collection classes in previous versions).

The TEMPLDEF Tool

The TEMPLDEF tool is a template expander. TEMPLDEF uses a syntax similar to the ANSI language syntax. In order to use TEMPLDEF, you must build it first, much like any other sample. This sample is provided only for backward compatibility, since with this version of Visual C++, true C++ templates are fully supported as are the MFC template classes that use them (see CList, CArray, and CMap classes)

A good way to get started is by examining four .CTT template classes provided with MFC (found in MFC Programming Utilities sample subdirectory). The MKCOLL.BAT batch file gives examples of the command line syntax of the TEMPLDEF tool. The general form of the command line syntax is modeled after a C++ typedef of the equivalent template class. For example,

typedef CLinkedList<CMyObj> CMyLinkedList;

becomes

TEMPLDEF "CLinkedList<CMyObj> CMyLinkedList" LINKLIST.CTT MYLIST.H MYLIST.INL MYLIST.CPP

The MFC template classes are slightly more complicated than this example, taking a number of Boolean-constant template parameters that allow several slightly different variations of collections to be expanded. Please refer to the sample for more details on the TEMPLDEF program and the command line options for using the provided template classes.

Technical Notes by NumberTechnical Notes by Category