A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2Ftemplate%2Ftemplate_function</id>
		<title>C++ Tutorial/template/template function - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2Ftemplate%2Ftemplate_function"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/template/template_function&amp;action=history"/>
		<updated>2026-04-07T22:52:24Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/template/template_function&amp;diff=2585&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/template/template_function&amp;diff=2585&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/template/template_function&amp;diff=2586&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/template/template_function&amp;diff=2586&amp;oldid=prev"/>
				<updated>2010-05-25T10:30:02Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==function template: GetMax==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream.h&amp;gt;&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
T GetMax (T a, T b) {&lt;br /&gt;
  T result;&lt;br /&gt;
  result = (a&amp;gt;b)? a : b;&lt;br /&gt;
  return (result);&lt;br /&gt;
}&lt;br /&gt;
int main () {&lt;br /&gt;
  int i=5, j=6, k;&lt;br /&gt;
  long l=10, m=5, n;&lt;br /&gt;
  k = GetMax&amp;lt; int &amp;gt;(i,j);&lt;br /&gt;
  n = GetMax&amp;lt; long &amp;gt;(l,m);&lt;br /&gt;
  cout &amp;lt;&amp;lt; k &amp;lt;&amp;lt; endl;&lt;br /&gt;
  cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;6&lt;br /&gt;
10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Function template: swapargs==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
// This is a function template. &lt;br /&gt;
template &amp;lt;class X&amp;gt; void swapargs(X &amp;amp;a, X &amp;amp;b) &lt;br /&gt;
{ &lt;br /&gt;
  X temp; &lt;br /&gt;
 &lt;br /&gt;
  temp = a; &lt;br /&gt;
  a = b; &lt;br /&gt;
  b = temp; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  int i=1, j=2; &lt;br /&gt;
  float x=1.1, y=2.3; &lt;br /&gt;
  char a=&amp;quot;x&amp;quot;, b=&amp;quot;z&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Original i, j: &amp;quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; j &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  swapargs(i, j); // swap integers &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Swapped i, j: &amp;quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; j &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Original x, y: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  swapargs(x, y); // swap floats &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Swapped x, y: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Original a, b: &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; b &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  swapargs(a, b); // swap chars &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Swapped a, b: &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; b &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;6&lt;br /&gt;
10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Function with generic parameters==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
template &amp;lt;class type1, class type2&amp;gt;&lt;br /&gt;
void f(type1 x, type2 y)&lt;br /&gt;
{&lt;br /&gt;
  cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  f(10, &amp;quot;AAAAA&amp;quot;);&lt;br /&gt;
  f(98.6, 19L);&lt;br /&gt;
  &lt;br /&gt;
  f(&amp;quot;C&amp;quot;, &amp;quot;V&amp;quot;);&lt;br /&gt;
  f(&amp;quot;C&amp;quot;, 0);&lt;br /&gt;
  &lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;10 AAAAA&lt;br /&gt;
98.6 19&lt;br /&gt;
C V&lt;br /&gt;
C 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Namespace with template function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;C++ Templates - The Complete Guide&amp;quot;&lt;br /&gt;
 * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
namespace X {&lt;br /&gt;
    template&amp;lt;typename T&amp;gt; void f(T);&lt;br /&gt;
}&lt;br /&gt;
namespace N {&lt;br /&gt;
    using namespace X;&lt;br /&gt;
    enum E { e1 };&lt;br /&gt;
    void f(E) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; &amp;quot;N::f(N::E) called\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
void f(int)&lt;br /&gt;
{&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;::f(int) called\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    ::f(N::e1);  // qualified function name: no ADL&lt;br /&gt;
    f(N::e1);    // ordinary lookup finds ::f() and ADL finds N::f(),&lt;br /&gt;
}                //  the latter is preferred&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;::f(int) called&lt;br /&gt;
N::f(N::E) called&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Overload a function template declaration==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
template &amp;lt;class X&amp;gt; void f(X a)&lt;br /&gt;
{&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Inside f(X a)\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
template &amp;lt;class X, class Y&amp;gt; void f(X a, Y b)&lt;br /&gt;
{&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Inside f(X a, Y b)\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  f(10);     // calls f(X)&lt;br /&gt;
  f(10, 20); // calls f(X, Y)&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Inside f(X a)&lt;br /&gt;
Inside f(X a, Y b)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Overload template function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;C++ Templates - The Complete Guide&amp;quot;&lt;br /&gt;
 * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
template&amp;lt;typename T&amp;gt; &lt;br /&gt;
std::string f(T) &lt;br /&gt;
{ &lt;br /&gt;
    return &amp;quot;Template&amp;quot;; &lt;br /&gt;
}&lt;br /&gt;
std::string f(int&amp;amp;) &lt;br /&gt;
{ &lt;br /&gt;
    return &amp;quot;Nontemplate&amp;quot;; &lt;br /&gt;
}&lt;br /&gt;
int main() &lt;br /&gt;
{&lt;br /&gt;
    int x = 7;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; f(x) &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Nontemplate&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==reference and non-reference template function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;C++ Templates - The Complete Guide&amp;quot;&lt;br /&gt;
 * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
void ref (T const&amp;amp; x)&lt;br /&gt;
{&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;x in ref(T const&amp;amp;): &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; typeid(x).name() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
void nonref (T x)&lt;br /&gt;
{&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; &amp;quot;x in nonref(T):     &amp;quot;&lt;br /&gt;
              &amp;lt;&amp;lt; typeid(x).name() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    ref(&amp;quot;hello&amp;quot;);&lt;br /&gt;
    nonref(&amp;quot;hello&amp;quot;);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;x in ref(T const&amp;amp;): A6_c&lt;br /&gt;
x in nonref(T):     PKc&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Specify template argument explicitly and implicitly==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;C++ Templates - The Complete Guide&amp;quot;&lt;br /&gt;
 * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
inline T const&amp;amp; max (T const&amp;amp; a, T const&amp;amp; b)&lt;br /&gt;
{&lt;br /&gt;
    return  a &amp;lt; b ? b : a;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    max&amp;lt;double&amp;gt;(1.0, -3.0);  // explicitly specify template argument&lt;br /&gt;
    max(1.0, -3.0);          // template argument is implicitly deduced&lt;br /&gt;
                             // to be double&lt;br /&gt;
    max&amp;lt;int&amp;gt;(1.0, 3.0);      // the explicit &amp;lt;int&amp;gt; inhibits the deduction;&lt;br /&gt;
                             // hence the result has type int&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==template function to display the number limits==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;limits&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
template&amp;lt;typename T&amp;gt;&lt;br /&gt;
void showMinMax( ) {&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;min: &amp;quot; &amp;lt;&amp;lt; numeric_limits&amp;lt;T&amp;gt;::min( ) &amp;lt;&amp;lt; endl;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;max: &amp;quot; &amp;lt;&amp;lt; numeric_limits&amp;lt;T&amp;gt;::max( ) &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
int main( ) {&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;short:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;short&amp;gt;( );&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;int:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;int&amp;gt;( );&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;long:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;long&amp;gt;( );&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;float:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;float&amp;gt;( );&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;double:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;double&amp;gt;( );&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;long double:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;long double&amp;gt;( );&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;unsigned short:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;unsigned short&amp;gt;( );&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;unsigned int:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;unsigned int&amp;gt;( );&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;unsigned long:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  showMinMax&amp;lt;unsigned long&amp;gt;( );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;short:&lt;br /&gt;
min: -32768&lt;br /&gt;
max: 32767&lt;br /&gt;
int:&lt;br /&gt;
min: -2147483648&lt;br /&gt;
max: 2147483647&lt;br /&gt;
long:&lt;br /&gt;
min: -2147483648&lt;br /&gt;
max: 2147483647&lt;br /&gt;
float:&lt;br /&gt;
min: 1.17549e-038&lt;br /&gt;
max: 3.40282e+038&lt;br /&gt;
double:&lt;br /&gt;
min: 2.22507e-308&lt;br /&gt;
max: 1.79769e+308&lt;br /&gt;
long double:&lt;br /&gt;
min: 0&lt;br /&gt;
max: 1.#INF&lt;br /&gt;
unsigned short:&lt;br /&gt;
min: 0&lt;br /&gt;
max: 65535&lt;br /&gt;
unsigned int:&lt;br /&gt;
min: 0&lt;br /&gt;
max: 4294967295&lt;br /&gt;
unsigned long:&lt;br /&gt;
min: 0&lt;br /&gt;
max: 4294967295&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==template function to get the maximum of three values of any type (call-by-reference)==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;C++ Templates - The Complete Guide&amp;quot;&lt;br /&gt;
 * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstring&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
// maximum of two values of any type (call-by-reference)&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
inline T const&amp;amp; max (T const&amp;amp; a, T const&amp;amp; b)&lt;br /&gt;
{&lt;br /&gt;
    return  a &amp;lt; b  ?  b : a;&lt;br /&gt;
}&lt;br /&gt;
// maximum of two C-strings (call-by-value)&lt;br /&gt;
inline char const* max (char const* a, char const* b)&lt;br /&gt;
{&lt;br /&gt;
    return  std::strcmp(a,b) &amp;lt; 0  ?  b : a;&lt;br /&gt;
}&lt;br /&gt;
// maximum of three values of any type (call-by-reference)&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
inline T const&amp;amp; max (T const&amp;amp; a, T const&amp;amp; b, T const&amp;amp; c)&lt;br /&gt;
{&lt;br /&gt;
    return max (max(a,b), c);  // error, if max(a,b) uses call-by-value&lt;br /&gt;
}&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; ::max(7, 42, 68);     // OK&lt;br /&gt;
    const char* s1 = &amp;quot;frederic&amp;quot;;&lt;br /&gt;
    const char* s2 = &amp;quot;anica&amp;quot;;&lt;br /&gt;
    const char* s3 = &amp;quot;lucas&amp;quot;;&lt;br /&gt;
    //::max(s1, s2, s3);    // ERROR&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;68&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==template function to get the maximum of two values==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;C++ Templates - The Complete Guide&amp;quot;&lt;br /&gt;
 * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
// maximum of two int values&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
inline int const&amp;amp; max (int const&amp;amp; a, int const&amp;amp; b)&lt;br /&gt;
{&lt;br /&gt;
    return  a &amp;lt; b ? b : a;&lt;br /&gt;
}&lt;br /&gt;
// maximum of two values of any type&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
inline T const&amp;amp; max (T const&amp;amp; a, T const&amp;amp; b)&lt;br /&gt;
{&lt;br /&gt;
    return  a &amp;lt; b ? b : a;&lt;br /&gt;
}&lt;br /&gt;
// maximum of three values of any type&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
inline T const&amp;amp; max (T const&amp;amp; a, T const&amp;amp; b, T const&amp;amp; c)&lt;br /&gt;
{&lt;br /&gt;
    return ::max (::max(a,b), c);&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    cout &amp;lt;&amp;lt;&amp;quot;\n\n&amp;quot; &amp;lt;&amp;lt;::max(7, 42, 68);     // calls the template for three arguments&lt;br /&gt;
    cout &amp;lt;&amp;lt;&amp;quot;\n\n&amp;quot; &amp;lt;&amp;lt;::max(7.0, 42.0);     // calls max&amp;lt;double&amp;gt; (by argument deduction)&lt;br /&gt;
    cout &amp;lt;&amp;lt;&amp;quot;\n\n&amp;quot; &amp;lt;&amp;lt;::max(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;);      // calls max&amp;lt;char&amp;gt; (by argument deduction)&lt;br /&gt;
    cout &amp;lt;&amp;lt;&amp;quot;\n\n&amp;quot; &amp;lt;&amp;lt;::max(7, 42);         // calls the nontemplate for two ints&lt;br /&gt;
    cout &amp;lt;&amp;lt;&amp;quot;\n\n&amp;quot; &amp;lt;&amp;lt;::max&amp;lt;&amp;gt;(7, 42);       // calls max&amp;lt;int&amp;gt; (by argument deduction)&lt;br /&gt;
    cout &amp;lt;&amp;lt;&amp;quot;\n\n&amp;quot; &amp;lt;&amp;lt;::max&amp;lt;double&amp;gt;(7, 42); // calls max&amp;lt;double&amp;gt; (no argument deduction)&lt;br /&gt;
    cout &amp;lt;&amp;lt;&amp;quot;\n\n&amp;quot; &amp;lt;&amp;lt;::max(&amp;quot;a&amp;quot;, 42.7);     // calls the nontemplate for two ints&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;68&lt;br /&gt;
42&lt;br /&gt;
b&lt;br /&gt;
42&lt;br /&gt;
42&lt;br /&gt;
42&lt;br /&gt;
97&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==template function to print elements of an STL container==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;C++ Templates - The Complete Guide&amp;quot;&lt;br /&gt;
 * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
// print elements of an STL container&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
void printcoll (T const&amp;amp; coll)&lt;br /&gt;
{&lt;br /&gt;
    typename T::const_iterator pos;  // iterator to iterate over coll&lt;br /&gt;
    typename T::const_iterator end(coll.end());  // end position&lt;br /&gt;
    for (pos=coll.begin(); pos!=end; ++pos) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; *pos &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v;&lt;br /&gt;
    v.push_back(42);&lt;br /&gt;
    v.push_back(13);&lt;br /&gt;
    v.push_back(7);&lt;br /&gt;
    printcoll(v);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;42 13 7&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==template type==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
template &amp;lt;class Type1, class Type2&amp;gt; &lt;br /&gt;
void myfunc(Type1 x, Type2 y) &lt;br /&gt;
{ &lt;br /&gt;
  cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  myfunc(10, &amp;quot;hi&amp;quot;); &lt;br /&gt;
 &lt;br /&gt;
  myfunc(0.23, 10L); &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;10 hi&lt;br /&gt;
0.23 10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using standard parameters in a template function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
const int TABWIDTH = 8;&lt;br /&gt;
template&amp;lt;class X&amp;gt; void f(X data, int tab)&lt;br /&gt;
{&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;tab=&amp;quot; &amp;lt;&amp;lt; tab;&lt;br /&gt;
    &lt;br /&gt;
    cout &amp;lt;&amp;lt; data &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  f(&amp;quot;This is a test&amp;quot;, 0);&lt;br /&gt;
  f(100, 1);&lt;br /&gt;
  f(&amp;quot;X&amp;quot;, 2);&lt;br /&gt;
  f(10/3, 3);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;19525391970This is a test&lt;br /&gt;
19525391971100&lt;br /&gt;
19525391972X&lt;br /&gt;
195253919733&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>