C++ Tutorial: Function Templates. Overloaded functions normally perform similar or identical operations on different types of data. If the operations are identical for each type, they can be expressed more compactly and conveniently using function templates. This tutorial introduces function templates. The need for function templates. In previous chapters, you’ve learned how to write functions and classes that help make programs easier to write, safer, and more. Last updated Jan 1, 2003. A function template declaration contains the keyword template, followed by a list of template parameters and a function. The Wolfram Language lets you insert function templates that contain placeholders for the arguments of a function. The tutorial is intended for students and developers who are already familar with C++ functions and function overloading. These. tutorials may refer to other chapters or sections of the book that are not included here. If the operations are identical for each type, they can be expressed more compactly and conveniently using function templates. Initially, the programmer writes a single function- template definition. Based on the argument types provided explicitly or inferred from calls to this function, the compiler generates separate object- code functions (i. In C, this task can be performed using macros created with the preprocessor directive #define (see Appendix F, Preprocessor). However, macros can have serious side effects and do not enable the compiler to perform type checking. Function templates provide a compact solution, like macros, but enable full type checking. Error- Prevention Tip 1. Function templates, like macros, enable software reuse. Unlike macros, function templates help eliminate many types of errors through the scrutiny of full C++ type checking. All function- template definitions begin with keyword template followed by a list of template parameters to the function template enclosed in angle brackets(< and > ); each template parameter that represents a type must be preceded by either of the interchangeable keywords class or typename, as intemplate< typename T > ortemplate< class Element. Type > ortemplate< typename Border. Type, typename Fill. Type > The type template parameters of a function- template definition are used to specify the types of the arguments to the function, to specify the return type of the function and to declare variables within the function. The function definition follows and appears like any other function definition. Note that keywords typename and class used to specify function- template parameters actually mean . Function template print. Array declares (line 8) a single template parameter T (T can be any valid identifier) for the type of the array to be printed by function print. Array; T is referred to as a type template parameter, or type parameter. Example: Function Template printArray. Let us examine function template printArray in Fig. Function template printArray declares (line 8) a single. Conversion of function arguments is allowed in function templates for any argument and parameter pair where the parameter does not depend on a template argument. Template Monster is a world leader in e-commerce - providing customers with valuable, convenient, relevant and enjoyable online experiences through a diversity of. C++ Templates - Learning C++ in simple and easy steps : A beginner's tutorial containing complete knowledge of C++ Syntax Object Oriented Language, Methods. Explicit instantiation of a function template or of a member function of a class template. Two function templates with the same return type and the same. You will see nontype template parameters in Section 1. When the compiler detects a print. Array function invocation in the client program (e. Array that best matches the function call. In this case, the only print. Array function with the appropriate number of parameters is the print. Array function template (lines 8–1. Consider the function call at line 3. The compiler compares the type of print. Array's first argument (int* at line 3. Array function template's first parameter (const. T* at line 9) and deduces that replacing the type parameter T with int would make the argument match the parameter. Then, the compiler substitutes int for T throughout the template definition and compiles a print. Array specialization that can display an array of int values. Array specializations—one that expects an int array, one that expects a double array and one that expects a char array. For example, the function- template specialization for type int isvoid print. Array( const int *array, int count ). Template parameter names among function templates need not be unique. T> 9 void print. Array( const T *array, int count ) 1. The program begins by declaring five- element int array a, seven- element double array b and six- element char array c (lines 2. Then, the program outputs each array by calling print. Array—once with a first argument a of type int* (line 3. The call in line 3. T is int and to instantiate a print. Array function- template specialization, for which type parameter T is int. The call in line 3. T is double and to instantiate a second print. Array function- template specialization, for which type parameter T is double. The call in line 4. T is char and to instantiate a third print. Array function- template specialization, for which type parameter T is char. It is important to note that if T (line 8) represents a user- defined type (which it does not in Fig. Common Programming Error 1. If a template is invoked with a user- defined type, and if that template uses functions or operators (e. Forgetting to overload such operators causes compilation errors. In this example, the template mechanism saves the programmer from having to write three separate overloaded functions with prototypesvoid print. Array( const int *, int ). Array( const double *, int ). Array( const char *, int ); that all use the same code, except for type T (as used in line 9). Performance Tip 1. Although templates offer software- reusability benefits, remember that multiple function- template specializations and class- template specializations are instantiated in a program (at compile time), despite the fact that the template is written only once. These copies can consume considerable memory. This is not normally an issue, though, because the code generated by the template is the same size as the code the programmer would have written to produce the separate overloaded functions.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |