
Introduction to Core C++ Lecture 04: Template Functions and Template Classes Massimiliano Culpo1 1CINECA - SuperComputing Applications and Innovation Department 26.02.2013 M.Culpo (CINECA) Introduction to C++ 26.02.2013 1 / 19 Template functions Template declarations Template declarations (x14) A template defines a family of classes or functions: 1 template <typenameT > 2 T const& max (T const& a , T const& b) f 3 // ifa < b then useb else usea 4 returna < b ? b : a ; 5 g A template declaration shall: declare or define a function or a class define a member function, a member class or a static data member define a member template of a class or class template A template declaration is also a definition if its declaration defines a function, a class, or a static data member. M.Culpo (CINECA) Introduction to C++ 26.02.2013 2 / 19 Template functions Template parameters Template parameters (x14.1) There are three kinds of allowed template parameters: non-type template parameters, type template parameters and template template parameters. A non-type template parameter shall be either an integral or enumeration type, a pointer/reference to object or function or a pointer to member. 1 template<constX & x , i n ti > voidf () f 2 i ++; // error 3 &x ; // OK 4 &i ; // error 5 i n t& r i = i ; // error 6 const int& c r i = i ; // OK 7 g A non-type template parameter cannot be assigned to or in any other way have its value changed. M.Culpo (CINECA) Introduction to C++ 26.02.2013 3 / 19 Template functions Template parameters Template parameters (x14.1) The keyword class followed by an unqualified-id names a type parameter: 1 template<c l a s sK , c l a s sV > c l a s s Map f 2 std :: vector<K> key ; 3 std :: vector<V> value ; 4 g ; Template classes may also be used as template parameters: 1 template<c l a s sK , c l a s sV , 2 template<c l a s sT > c l a s sC > c l a s s Map f 3 C<K> key ; 4 C<V> value ; 5 g ; M.Culpo (CINECA) Introduction to C++ 26.02.2013 4 / 19 Template functions Template parameters Template parameters (x14.1) A default argument may be specified for any kind of template parameter: 1 template<c l a s s T1, c l a s s T2 = int > c l a s sA ; 2 3 template <c l a s sT = f l o a t > s t r u c tB f g ; 4 template <template <c l a s s TT = f l o a t > c l a s sT > 5 s t r u c tA f 6 i n l i n e voidf (); 7 i n l i n e voidg (); 8 g ; 9 template <template <c l a s s TT> c l a s sT > 10 voidA <T>:: f () f T<> t ; g // error 11 template <template <c l a s s TT = char> c l a s sT > 12 voidA <T>:: g () f T<> t ; g // OK − T<char> M.Culpo (CINECA) Introduction to C++ 26.02.2013 5 / 19 Template functions Function templates Function templates (x14.5.6) A function template defines an unbounded set of related functions: 1 // sort.hxx 2 template<c l a s sT > void sort ( T& input ); A function template can be overloaded: 1 with other function templates 2 with normal functions The mere use of a function template can trigger an instantiation: 1 #include "sort.hxx" 2 std :: vector< double > vec ; 3 s o r t ( vec ); M.Culpo (CINECA) Introduction to C++ 26.02.2013 6 / 19 Template functions Function templates Overloading of function templates (x14.5.6) 1 // max.hxx 2 3 // maximum of two int values 4 i n l i n e int const& max ( i n t const& a , i n t const& b ) 5 f r e t u r na <b?b : a ; g 6 7 // maximum of two values of any type 8 template <typenameT > 9 i n l i n eT const & max (T const& a , T const& b ) 10 f r e t u r na <b?b : a ; g 11 12 // maximum of three values of any type 13 template <typenameT > 14 i n l i n eT const & 15 max (T const& a , T const& b , T const& c ) 16 f r e t u r n max (max( a , b ), c ); g M.Culpo (CINECA) Introduction to C++ 26.02.2013 7 / 19 Template functions Function templates Overloading of function templates (x14.5.6) 1 #include "max.hxx" 2 3 i n t main () f 4 :: max( 7 , 42 , 6 8 ) ; // template for three arguments 5 :: max (7.0, 42.0); // max<double> 6 :: max('a','b'); // max<char> 7 :: max( 7 , 4 2 ) ; // non−template for two ints 8 :: max<>(7, 4 2 ) ; // max<int > 9 :: max<double >(7 , 4 2 ) ; // max<double> 10 :: max('a', 42.7); // non−template for two ints 11 g M.Culpo (CINECA) Introduction to C++ 26.02.2013 8 / 19 Template functions Class templates Class templates (x14.5.1) A class template defines an unbounded set of related types: 1 template<c l a s sT > c l a s s Array f 2 T∗ v ; 3 i n t sz ; 4 p u b l i c : 5 e x p l i c i t Array ( i n t ); 6 T& operator []( i n t ); 7 T& elem ( i n ti ) f r e t u r nv [ i ]; g 8 g ; A member function may be defined outside the class in which it is declared: 1 template<c l a s sT > T& Array<T>:: operator []( i n ti ) f 2 i f ( i <0 j j sz<=i ) e r r o r ("Array: range error"); 3 r e t u r nv [ i ]; 4 g M.Culpo (CINECA) Introduction to C++ 26.02.2013 9 / 19 Template functions Class templates Class templates (x14.5.1) Similar rules apply to class member: 1 template<c l a s sT > s t r u c tA f c l a s sB ; g ; 2 A<int >::B∗ b1 ; 3 template<c l a s sT > c l a s sA <T>::B f g ; 4 A<int >::B b2 ; For a member function of a class template: 1 Array<int > v1 ( 2 0 ) ; 2 Array<dcomplex> v2 ( 3 0 ) ; 3 // Array<int >::operator[]() 4 v1 [ 3 ] = 7 ; 5 // Array<dcomplex >::operator[]() 6 v2 [ 3 ] = dcomplex ( 7 , 8 ) ; the template arguments are determined by the type of the object for which the member function is called. M.Culpo (CINECA) Introduction to C++ 26.02.2013 10 / 19 Template functions Class templates Member template (x14.5.2) A template can be declared within a class or class template: 1 template<c l a s sT > s t r u c t string f 2 3 template<c l a s s T2> 4 i n t compare ( const T2&); 5 6 template<c l a s s T2> 7 s t r i n g ( const string <T2>& s ) 8 f /∗ ... ∗/ g 9 g ; 10 11 template<c l a s sT > template<c l a s s T2> 12 i n t string <T>:: compare ( const T2& s ) 13 f /∗ ... ∗/ g M.Culpo (CINECA) Introduction to C++ 26.02.2013 11 / 19 Template functions Class templates Member template (x14.5.2) A local class shall not have member templates. A member function template: 1 shall not be virtual 2 does not override a virtual function 1 template <c l a s sT > s t r u c t AA f 2 template <c l a s sC > v i r t u a l voidg (C ); // error 3 g ; 4 c l a s sB f 5 v i r t u a l voidf ( i n t ); 6 g ; 7 c l a s sD : p u b l i cB f 8 template <c l a s sT > voidf (T); // no override 9 voidf ( i n ti ) f f <>( i ); g // overriding 10 g ; M.Culpo (CINECA) Introduction to C++ 26.02.2013 12 / 19 Template functions Class templates Class template partial specialization (x14.5.5) A class template may be partially specialized: 1 template<c l a s s T1, c l a s s T2, i n tI > 2 c l a s sA f g ; // Primary class template 3 4 template<c l a s sT , i n tI > 5 c l a s sA <T, T∗ , I> f g ; // #1 6 7 template<c l a s s T1, c l a s s T2, i n tI > 8 c l a s sA <T1∗ , T2, I> f g ; // #2 Each class template partial specialization is a distinct template and definitions shall be provided for its members. M.Culpo (CINECA) Introduction to C++ 26.02.2013 13 / 19 Template functions Name resolution The typename keyword (x14.6) The keyword typename clarifies that an identifier is a type: 1 template <c l a s sT > 2 c l a s sA f 3 typenameT :: SubType ∗ ptr ; 4 /∗ ... ∗/ 5 g ; Without typename, the expression: 1 T :: SubType ∗ ptr ; would be a multiplication of a static member of class T with ptr. In general, typename has to be used whenever a name that depends on a template parameter is a type.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages19 Page
-
File Size-