How to Instantiate a Templated FunctionLast reviewed: July 31, 1997Article ID: Q131014 |
2.00 2.10 4.00
WINDOWS NT
kbtool kbprg kbcode
The information in this article applies to:
SUMMARYThere is no built-in way to create an explicit instantiation of a templated function. Normally, an instantiation is created when the function is called the first time with a specific type. Subsequent calls to the function with the same type use the same piece of the code generated before. However, if you want to instantiate the function without actually calling it, you must use one of the methods in this article.
MORE INFORMATIONThe best method to use with function templates is to place the function definition in a header file, and include that header file in all of the source files. If this is not an option then try one of these alternatives:
Sample Code
/* Compile options needed: none */ /********** Source File One **********/ void func1(void);template < class T> T SwapTemplate(T Source) { return Source;}
void Dummy(void) { // Option One char a = 'a', b; b = SwapTemplate(a);}
void main() { int (*pfunc)(int) = SwapTemplate; // Option Two func1();}
/********** Source File Two **********/template < class T> T SwapTemplate(T Source); // Prototype
void func1(void) { int a = 5, b; char c = 'c', d; b = SwapTemplate( a ); d = SwapTemplate( c );}
|
Additional reference words: kbinf 9.00 9.0 9.1 9.10 2.00 2.10 4.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |