Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Allows you to write a single function that can work with different data types, provided they support the operations used inside the function.
template <typename T>
void genericSwap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int x = 5, y = 10; genericSwap(x, y);
string s1 = "A", s2 = "B"; genericSwap(s1, s2);The compiler can often deduce T automatically from the arguments you pass.
You can use multiple template parameters: template <typename T, typename U>.
If the compiler can't deduce the type, you can specify it manually:
genericSwap<int>(x, y);