I am quite new to the topic of templates in C++. Why in the following toy example code do we have to precede the class's and each function's name with template <class T>
(meaning why do we need it, at all)?
Is it possible to modify the code not to use template <class T>
everywhere?
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
using namespace std;
template <class T>
class Stack { private:
vector<T> elements;
public:
void push(T const &);
void pop();
T top();
bool empty(); };
template <class T>
void Stack<T>::push(T const &elem) {
elements.push_back(elem); }
template <class T>
void Stack<T>::pop() {
if (elements.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
} else {
elements.pop_back();
}
}
template <class T>
T Stack<T>::top() {
if (empty()) {
throw out_of_range("Stack<>::top(): empty stack");
}
return elements.back();
}
template <class T>
bool Stack<T>::empty() {
return elements.empty();
}
int main() {
try {
Stack<int> intStack; // Stack of ints
Stack<string> stringStack; // Stack of strings
// Manipulate integer stack
intStack.push(7);
cout << intStack.top() << endl;
// Manipulate string stack
stringStack.push("hello");
cout << stringStack.top() << std::endl;
stringStack.pop();
stringStack.pop();
}
catch (exception const &ex) {
cerr << "Exception: " << ex.what() << endl;
return -1;
}
}