Chapter Contents |
Previous |
Next |
Templates |
The SAS/C C++ Development System supports function templates with nontype parameters. Refer to Template Parameters for a description of nontype parameters. You can use nontype parameters to declare the template parameters and return types in a function template declaration. An example follows:
template <int SZ> class Bitvect; template <int SZ1, int SZ2> Bitvect<SZ1+SZ2> concat( const Bitvect<SZ1>& b1, const Bitvect<SZ1>& b2 );
Nonmember function templates cannot have default arguments.
Overloading Function Templates |
Deducing Arguments |
template <class T> void f( T& ); void testit( int i ) { f( i ); // calls f( int& ) }
template <class T> void f( T ); int a[5]; void testit() { f( a ); // calls f( int* ) }
template <class T> void f( const T* ); void testit( int* p ) { f( p ); // calls f( const int* ) }
template <class T> void f( typename T::Z* ); // T not deducible
Template parameters used in subexpressions in a template function declaration cannot be deduced either. However, parameters that are used directly as array bounds and class template arguments are deducible. Several examples follow:
template <int sz> void f1( int (*arrayPtr)[sz] ); // deducible template <int sz> void f2( int (*arrayPtr)[sz+1] ); // sz not // deducible template <class T, int SZ> class Vect; template <class T, int sz> void f3( Vect<T, sz>& v ); // deducible template <class T, int sz> void f4( Vect<T, sz+1>& v ); // sz not deducible template <class T> void f5( Vect<T, 8/sizeof(T) >& v ); // T not // deducible template <class T, int sz> void f6( Vect<T*, sz >& v ); // deducible
template <int size> void f7( int a[size][4] ); // size not // deducible, // same as "int (*a)[4]"
template <int N> void f8( int(*ap) [N+1]); template <int U> void f8( int(*ap) [U+1]); // redeclaration - // template parameters renamed
Specifying Arguments |
Template arguments for nonmember template functions may be specified in the form:
function_template_name< optional_template_args >;
The function template name is followed by a possibly empty (
< >
) template argument list.
template <class T, class U> inline T implicit_cast( U u ) { return u; }
void f( double ); void f( int ); template <class T> void callit( T t ) { // call f( double ) f( implicit_cast<double>( t ) ); }
template <class T> inline T Max( T x, T y ) { return (x > y ) ? x : y; } int compare( int i, char c ) { // Calling Max( i, c ) would fail because // the argument types differ too much, // and the template parameter cannot // be deduced. But you can explicitly // specify the type. Max<int>( i, c ); // OK, calls Max(int, int) }
template <class T> inline T Max( T x, T y ) { return (x > y ) ? x : y; } int Max( int, int ); // guiding declaration int compare( int i, char c ) { // Calling Max( i, c ) would fail without // the guiding declaration because the // argument types differ too much. Max( i, c ); // OK, calls Max(int, int) }
When the
tmplfunc
translator option is specified, nontemplate functions are
distinct from template specializations. Therefore, guiding declarations are
not recognized. In such cases, inline functions can be used in place of the
guiding declaration:
template <class T> inline T Max( T x, T y ) { return (x > y ) ? x : y; } // forwarding declaration inline int Max( int x, int y ) { return Max<>( x, y ); } // call the template // version int compare( int i, char c ) { // Calling Max( i, c ) would fail without // the forwarding declaration because the // argument types differ too much. Max( i, c ); // OK, calls Max(int, int) }
See Option Descriptions for a full description
of the
tmplfunc
option.
Function Template Signatures |
Functions with normal linkage conventions (not
extern "C"
) encode information about their parameter
types into their linkage name. For specializations of function templates,
this linkage information potentially includes the template arguments also.
When the template arguments are included, components such as COOL will print
the name including the template arguments using the function name followed
by the actual template arguments.
By default, the SAS/C C++ Development System uses the
nontemplate linkage name for specializations of normally deducible function
templates for compatibility with older code. Functions which are not deducible
(see message LSCT628 in
SAS/C Software Diagnostic Messages), which older compilers treat as erroneous,
always include the template parameters in their linkage. With the
tmplfunc
option, the linkage names of function
template specializations will be made distinct from nontemplate functions,
by including the template arguments for the specialization in the linkage
name.
Other Template Signatures |
< >
. Also the argument list may be omitted
in cases where there is not enough space to output the list completely.
enum Color { Red = 1, Blue = 2, Green = 4 }; template <Color clr> class C { public: static int si; };
A reference to
C<Red>::si
might be output as
C<1>::si
.
In the latter two cases, there may be no unique C++ expression that generates the given value, so a standard form was chosen.
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.