template

should i really have to know about templates?
Naturally, yes. Templates are used a lot in the C++ Standard Library.

Containers and Algorithms (which you'll find very useful, unless you're hellbent on reinventing the wheel) are implemented as class templates and function templates, respectively.

http://www.cplusplus.com/reference/stl/
http://www.cplusplus.com/reference/algorithm/

Templates allow you to write generic code.

The idea behind generic code revolves around letting the C++ compiler fill out the appropriate types in your code (as you write your algorithm to work identically for most types).

Non-template version of a max() function:

1
2
3
4
5
6
7
8
// good and clean, but only used for int's
int max(int a, int b)
{
    if (a > b)
        return a;

    return b;
}


The template version of the max() function:

1
2
3
4
5
6
7
8
template <typename Number>
Number max(Number a, Number b)
{
    if (a > b)
        return a;

    return b;
}


Notice how the algorithm inside the two versions is exactly the same.
However now, the max() function template will be used automatically for any type that can be compared by operator> without us having to write multiple versions of max().

Edit: also, the type name "Number" is just a hint to fellow programmers that the function template should be used with numbers. It doesn't stop them from using our max() on std::string's for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

namespace test
{

template <typename Number>
Number max(Number a, Number b)
{
    if (a > b)
        return a;

    return b;
}

}

int main()
{
    std::string s1("Andrew");
    std::string s2("Xavier");

    std::cout << test::max(s1, s2) << std::endl;
}
Xavier


Above the max() function template is put in the test namespace because there already exists max() in the std namespace, provided by the C++ Standard Library.

http://www.cplusplus.com/reference/algorithm/max/
Last edited on
also, it's useful to understand operator overloading as well because it will make template programming more powerful.
@Catfish666 in the above code why "andrew" didnot print out?
Last edited on
justinelandichoruiz wrote:
@Catfish666 in the above code why "andrew" didnot print out?


Because test::max() returns the "Xavier" string only.
Remember that a return statement "stops" a function.

1
2
3
4
5
6
7
8
9
10
void func()
{
    std::clog << "A\n";
    std::clog << "B\n";

    return; // void functions can return nothing

    std::clog << "C\n"; // "unreachable code"
    std::clog << "D\n"; // "unreachable code"
}
A
B


Because of the return statement, the function above won't print "ABCD".

1
2
3
4
5
6
7
8
9
template <typename Number>
Number max(Number a, Number b)
{
    if (a > b)
    // if this if() body is entered, "return b;" will never be reached
        return a;

    return b;
}

@catfish666 so that is how it is. i thought you should put a stop code after the code return a; just like break;.
back to the topic
in you're code the non template version is even working for max()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
namespace test
{

int number(int a, int b)
{
    if (a > b)
        return a;

    return b;
}

}

int main()
{
    std::string s1("Andrew");
    std::string s2("Xavier");

    std::cout << std::max(s1, s2) << std::endl;
}


in you're code the non template version is even working for max()


std::max is templated.

You seem to have std::max confused with test::number.
@cire
hahaha i see it was the function max() he did not mean std::max what an embarassment.

its clear now thank you
Last edited on
Topic archived. No new replies allowed.