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 (§14)
A template defines a family of classes or functions:
1 template
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 (§14.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
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 (§14.1)
The keyword class followed by an unqualified-id names a type parameter:
1 template
Template classes may also be used as template parameters:
1 template
M.Culpo (CINECA) Introduction to C++ 26.02.2013 4 / 19 Template functions Template parameters Template parameters (§14.1)
A default argument may be specified for any kind of template parameter:
1 template
M.Culpo (CINECA) Introduction to C++ 26.02.2013 5 / 19 Template functions Function templates Function templates (§14.5.6)
A function template defines an unbounded set of related functions:
1 // sort.hxx 2 template
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 (§14.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 { r e t u r na 9 i n l i n eT const & max (T const& a , T const& b ) 10 { r e t u r na 14 i n l i n eT const & 15 max (T const& a , T const& b , T const& c ) 16 { r e t u r n max (max( a , b ), c ); }
M.Culpo (CINECA) Introduction to C++ 26.02.2013 7 / 19 Template functions Function templates Overloading of function templates (§14.5.6)
1 #include ”max.hxx” 2 3 i n t main () { 4 :: max( 7 , 42 , 6 8 ) ; // template for three arguments 5 :: max (7.0, 42.0); // max
M.Culpo (CINECA) Introduction to C++ 26.02.2013 8 / 19 Template functions Class templates Class templates (§14.5.1)
A class template defines an unbounded set of related types:
1 template
A member function may be defined outside the class in which it is declared:
1 template
M.Culpo (CINECA) Introduction to C++ 26.02.2013 9 / 19 Template functions Class templates Class templates (§14.5.1)
Similar rules apply to class member:
1 template
For a member function of a class template:
1 Array
M.Culpo (CINECA) Introduction to C++ 26.02.2013 10 / 19 Template functions Class templates Member template (§14.5.2)
A template can be declared within a class or class template:
1 template
M.Culpo (CINECA) Introduction to C++ 26.02.2013 11 / 19 Template functions Class templates Member template (§14.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
M.Culpo (CINECA) Introduction to C++ 26.02.2013 12 / 19 Template functions Class templates Class template partial specialization (§14.5.5)
A class template may be partially specialized:
1 template
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 (§14.6)
The keyword typename clarifies that an identifier is a type:
1 template
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.
M.Culpo (CINECA) Introduction to C++ 26.02.2013 14 / 19 Template functions Name resolution The .template construct (§14.6)
Consider the following example:
1 template
Without that extra use of template, the compiler can’t deduce the meaning of the < token.
M.Culpo (CINECA) Introduction to C++ 26.02.2013 15 / 19 Template functions Name resolution Dependent names (§14.6.2)
If a base class depends on a template parameter, the base class scope is not examined during unqualified name lookup:
1 typedef doubleA ; 2 3 template
The consequence is that using a name x by itself is not always equivalent to this−>x.
M.Culpo (CINECA) Introduction to C++ 26.02.2013 16 / 19 Template functions Name resolution String literals
Q: Can you explain the following behavior?
1 template
M.Culpo (CINECA) Introduction to C++ 26.02.2013 17 / 19 Template functions Name resolution Explicit instantiation (§14.8.1)
An empty template argument list can be used to indicate that a given use refers to a specialization of a function template:
1 template
1 template
M.Culpo (CINECA) Introduction to C++ 26.02.2013 18 / 19 Template functions Summary overview of name resolution Summary: stages of function name resolution
When a function name is to be resolved:
1 foo ( 1 2 , 4 5 ) ;
the C++ machinery goes through a number of codified stages: 1. (compile-time) Name lookup (§3.4) 2. (compile-time) Template argument deduction (§14.8.2) 3. (compile-time) Overload resolution (§13) 4. (compile-time) Access control (§11) 5. (run-time) Virtual function resolution (§10.3-§10.4) It is important to know the subtleties of each of these stages to avoid common pitfalls.
M.Culpo (CINECA) Introduction to C++ 26.02.2013 19 / 19