CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012
Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................ 3 1.3 Templating Classes.......................................... 4 2 Ternary Operators in C++ 6 2.1 Basic Syntax............................................. 6
1. Templating in C++ 1.1 Motivation A common annoyance in programming is the need to duplicate a common function structure for multiple variable types (e.g. int, float, etc.). A way of working around this is through the use of templated functions. Where the template acts as a wild card in the code until the code is compiled. When compiled, the compiler parses the templated function, and substitutes in the necessary variable type. 1.2 Templating Functions The traditional way of creating overloaded functions for multiple variable types is shown below (see Section 2 for use of ternary conditional operator): double mymax ( double a, double b){ float mymax ( float a, float b){ int mymax ( int a, int b){ With templating we can create one function that will perform all of the same functionality for all all possible variable types, including user defined types. To do this we define a templated version of the function. We first need to define the templated variables, whose type is left purposefully undefined until the code is compiled. See below for example: template < typename T > T mymax (T a, T b){ Now we will invoke this function in the same way that we are used to. // using mymax for variable type double double a = 2, b = 4; double c; c = mymax (a,b); // using mymax for variable type int int a = 4, b = 1; int c; c = myamx (a,b); As we can see, templating is a powerful tool for reducing duplication of similar functions of different variable types in code. Templating can be extended to template using programmer-defined classes instead of the standard variable typenames to template on: template < class T > T mymax (T a, T b){ To learn more about templating and its extendibility see [1]. The use of non-type parameters is worthy of note. 3
1.3 Templating Classes A more useful feature of templating is that it can be used when definite classes. By so doing it is possible to write one class definition that can be implemented for various variable types without any extra effort. Below is an example of implementing our vector class with templating: # include < iostream > using namespace std ; template < class T, int lowest_ index > class vector { public : T * data ; int length ; vector ( int L){ length = L; data = new T[ length ]; vector ( const vector & other ){ length = other. length ; data = new T[ length ]; for ( int n =1;n <= length ;++ n){ operator ()(n) = other (n); ~ vector (){ if( data ){ delete [] data ; data = NULL ; T & operator () ( int n){ // 1- indexed return data [n- lowest_ index ]; T operator () ( int n) const { // 1- indexed return data [n- lowest_ index ]; // fingers crossed friend ostream & operator << ( ostream & os, vector <T, lowest_index > v){ ; for ( int n =1;n <=v. length ;++ n){ os << v( n) << endl ; return os; 4
Use of the templated class is done in much the same way. Only that now, when construction an instance of the class, one needs to explicitly declare the class type and any non-type parameters included in the class definition: main () { vector < int, 1 > iv (10) ; iv (4) = 6; cout << " iv =" << iv << endl ; 5
2. Ternary Operators in C++ 2.1 Basic Syntax With this lecture comes the introduction of the ternary operator in C++. The ternary operator is a syntax reduction, where a logical statement is first evaluated, then depending on the evaluation one of two outputs is returned, The implementation of a function that evaluates the maximum of two arguments would be implemented as follows: double mymax ( double a, double b){ // Evaluates logical statement a > b. // If TRUE returns a // If FALSE returns b When using the ternary operator first place the logical statement to be tested between the parentheses (). Next, identify the statement as a ternary conditional statement though the use of?. Finally, supply the two returns for each logical outcome. The return if true should be place before the return if false. Both returns need to be separated by a colon. For more information on the use of the ternary operator see [2]. 6
Bibliography [1] C++ templates. http://www.cplusplus.com/doc/tutorial/templates/. [2] How to use the conditional (ternary) operator. http://www.cplusplus.com/forum/articles/14631/. 7