-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionTemplates.cpp
More file actions
33 lines (27 loc) · 1.11 KB
/
functionTemplates.cpp
File metadata and controls
33 lines (27 loc) · 1.11 KB
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
29
30
31
32
33
#include <iostream>
namespace test
{
template <typename T> // Define 'T'
T maxOne(T x, T y)
{ // Changes data type to anything BUT 'T' can only be one thing for each function call, can accept one data type, returns type 'T'
return (x > y) ? x : y;
}
template <typename T, typename U> // Define 'T' and 'U'
T maxTwo(T x, U y)
{ // Changes data type to anything BUT 'T' and 'U'can oly be one thing for each function call, can accept two data types, returns type 'T'
return (x > y) ? x : y;
}
template <typename T, typename U> // Define 'T' and 'U'
auto maxThree(T x, U y)
{ // Changes data type to anything BUT 'T' and 'U'can oly be one thing for each function call, can accept two data types, automatically returns the right type
return (x > y) ? x : y;
}
}
int main()
{
std::cout << test::maxOne(1, 2) << '\n'; // Will return 2
std::cout << test::maxOne(1.1, 2.2) << '\n'; // Will return 2.2
std::cout << test::maxTwo(1, 2.2) << '\n'; // Will return 2
std::cout << test::maxThree(1, 2.2) << '\n'; // Will return 2.2
return 0;
}