How useful are templates in high level programming?

In some of the homework assignments that I have been given, I never really found a use for templates since regular functions pretty much accomplish the same thing. I know the code below is an overly simplified use of a template, but I couldn't really find a scenario that directly needs templates.

Can someone show me a good example that uses templates. By the way, is using templates even considered good practice for c++?

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

using namespace std;

template <class A, class B>
bool falsetrue (A a, B b)
{
    if (a==b)
        cout << "They are equal" << "\n";

    if (a!=b)
        cout << "Those numbers aren't equal!" << "\n";
}

int main ()
{
        int answer1,answer2;

        cout << "Insert two numbers that are equal to each other" << "\n" << "\n";
        cout << "Insert the first number:" << "\n";
        cin >> answer1;
        cout << "Now insert another number" << "\n";
        cin >> answer2;
        falsetrue (answer1,answer2);

        return 0;
}
Last edited on
Templates are extremely useful for an example just look at the standard template library
abel muro wrote:
Can someone show me a good example that uses templates
1
2
3
4
5
#include <iostream>
int main()
{
    std::cout << std::endl;
}

In this simple example, << is a member function of a class template and endl is a function template.
Last edited on
Topic archived. No new replies allowed.