Skip to content

Instantly share code, notes, and snippets.

@VenkataPavan2494
Created April 21, 2018 10:46
Show Gist options
  • Save VenkataPavan2494/fbe180c8d2245ae97c15a5295b46cea2 to your computer and use it in GitHub Desktop.
Save VenkataPavan2494/fbe180c8d2245ae97c15a5295b46cea2 to your computer and use it in GitHub Desktop.
simple_function_templates.cpp
// CPP program to demonstrate simple function template.
#include <iostream>
using namespace std;
template <typename T>
T my_max(T a, T b) {
return ((a > b)?a:b);
}
int main(int argc, char ** argv) {
// Finding max between two integers of unsigned 16 bit.
uint16_t a = 10, b = 23;
cout << "Max of a(" << a << ") and b(" << b << ") is " << my_max(a, b) << endl;
// Finding max between two integers of unsigned 32 bit.
// Important note is that, compiler internally generates two different functions
// for type uint16_t and uint32_t, though both are just variations of type interger.
uint32_t p = 23232, q = 43433;
cout << "Max of p(" << p << ") and q(" << q << ") is " << my_max(p, q) << endl;
// float data type..
float f = 12.34, g = 34.45;
cout << "Max of f(" << f << ") and g(" << g << ") is " << my_max(f, g) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment