Template variables in C++

c++, templates

Solution

In C++11, you can use `auto`:

auto k = GetMax(i,j);
auto n = GetMax(l,m);

The types of `k` and `n` are deduced from the expression used to initialise them.

Prior to C++11, you would need to give the types explicitly. However, you should always be able to write the types in some form or another, since you know the types of the arguments.

Problem

I have this code: ``` template <class T> T GetMax (T a, T b) { return (a>b?a:b); } int main () { int i=51, j=26, k; long l=100, m=15, n; k=GetMax(i,j); n=GetMax(l,m); cout << k << endl; cout << n << endl; return 0; } ``` How can I change the data type of variables k and n so that they can be dynamic enough to accept the returned value. If the returned value is a double, the k and n will automatically be double, so I need not bother whether I am passing in int or double. I tried searching it in online and in my books but no luck. Can you Plz help me? I am new to templates.

Original source