ILE C/C++ Programmer's Guide

Structuring a Program for TEMPINC-Managed Instantiations

When you use the TEMPINC instantiation management option, follow these guidelines as you structure a program:

The Template-Implementation File

In the Stack source, the file stack.c is a template-implementation file. To create a program using the Stack class template, both stack.h and stack.c must reside in the same directory. You include stack.h in any source files that use an instance of the class. The stack.c file does not need to be included in any source files. For example:

Figure 323. Example of Template-Implementation File



#include "stack.h"
void Swap(int i&, Stack<int,20>& s)
{
int j;
s >> j;
s << i;
i = j;
}

The compiler automatically generates the functions
Stack<int,20>::operator<<(int) and
Stack<int,20>::operator>>(int&).

Renaming or Relocating the Template-Implementation file

Use the #pragma implementation directive to change the name of the template-implementation file or place it in a different directory .

The syntax is:

>>-#pragma--implementation--"path"-----------------------------><
 
 

Notes:

  1. The path is used to specify the path name for the template-implementation file. If it is only a partial path name, it must be relative to the directory containing the header file.

  2. This path is a quoted string following the normal conventions for writing string literals. Backslashes must be doubled.

Changing the Template-Implementation file

To use the file stack.def as the template-implementation file instead of stack.c, add the line: #pragma implementation("stack.def") anywhere in the stack.h file, in the Stack class. The compiler then looks for the template-implementation file stack.def in the same directory as stack.h.

Tempinc Files

If you use the TEMPINC template instantiation management option, the tempinc file is generated by the compiler.

Note:
Do not edit tempinc files.

Figure 324 shows a typical tempinc file generated by the compiler.

Figure 324. A Typical tempinc File


1   /*0000000000*/ #pragma sourcedir("c:\swearsee\src") 
2   /*0698421265*/ #include "\swearsee\src\list.h" 
3   /*0000000000*/ #include "\swearsee\src\list.c" 
4   /*0698414046*/ #include "\swearsee\src\mytype.h" 
5   /*0698414046*/ #include "/QIBM/include/iostream.h" 
6   template void List <MyType>::push(MyType); 
7   template MyType List<MyType>::pop(); 
8   ostream& operator<<(ostream&,List<MyType>); 
9   #pragma undeclared 
10  int count(List<MyType>); 

Notes:

  1. This pragma ensures that the compiler looks for nested include files in the directory containing the original source file, as required by the ILE C++ file inclusion rules.

  2. The header file that corresponds to the tempinc file. The number in comments at the start of each #include line (for this line /*0698421265*/) is a time stamp for the included file. The compiler uses this number to determine if the tempinc file is current or should be recompiled. A time stamp containing only zeroes (0) as in line 3 means the compiler is to ignore the time stamp.

  3. The template-implementation file that corresponds to the header file in line 2.

  4. Another header file that the compiler requires to compile the tempinc file. All other header files that the compiler needs to compile the tempinc file are inserted at this point.

  5. Another header file required by the compiler. It is referenced in the function declaration in lines 6-7.

  6. The first statement of the explicit instantiation: In this case, the class List<MyType> is to be defined.

  7. The second statement of the explicit instantiation: In this case, the class List<MyType> member functions are to be generated.

  8. The operator<< function is a nonmember function that matched a template declaration in the list.h header file. The compiler inserts this declaration to force the generation of the function definition.

  9. The #pragma undeclared directive is used only by the compiler and only in tempinc files. All function templates that are explicitly declared in at least one compilation unit appear before this line. All function templates that are called, but never declared, appear after this line. This division is necessary because the C++ rules for function overload resolution treat declared and undeclared function templates differently.

  10. count is a function template that is called but not declared. The template declaration of the function is contained in list.h, but the instance count(List<MyType>) is never declared.


[ Top of Page | Previous Page | Next Page | Table of Contents ]