# Thread: How to define a polymorphic function for numeric values

1. ## How to define a polymorphic function for numeric values

Hi,

Sorry for this question if it's too basic but I'm just a C programmer starting with C++, I want to define this polymorphic function __max3 to work for all the different types of numeric values.

Like the built in function __max:
Code:
```type __max(
type a,
type b
);```
This is what my old C code for this was (now I want to do it a bit more clean with the function):
Code:
`#define __max3(x1,x2,x3) __max(x1,max(x2,x3))`
I've tried this but doesn't work:
Code:
```type __max3(type a, type b, type c)
{
return ( __max(a,max(b,c)) );
}```
On another note, if I later want to make this function to be inlined with __forceinline, will it still work with the polymorphic definition?

TIA & Regards ...

2. What built-in function __max?

Anyway, I can't see any polymorphism here, but I do see templates.

3. Originally Posted by tabstop
What built-in function __max?
The one in Visual Studio C++, defined in stdlib.h
Originally Posted by tabstop
Anyway, I can't see any polymorphism here, but I do see templates.
Thanks, that's seemed to be a great pointer, I called it polymorphism but it seems it's a template.

Would this be the proper way to do it?

Code:
`template <class T>	T __newmax3(T a, T b, T c);`
// The actual code
Code:
```template <class T>
T __newmax3(T a, T b, T c)
{
return ( __max(a,__max(b,c)) );
}```
TIA & Regards ...

Well, that's interesting, but it's just "max" without the __ bit.

Edit to add: Mainly because you shouldn't be using stdlib.h in C++, as it's a C header. You can use cstdlib if you need something from it, but max is actually in <algorithm> of all places.
Thanks, that's seemed to be a great pointer, I called it polymorphism but it seems it's a template.

Would this be the proper way to do it?

Code:
`template <class T>	T __newmax3(T a, T b, T c);`
// The actual code
Code:
```template <class T>
T __newmax3(T a, T b, T c)
{
return ( __max(a,__max(b,c)) );
}```
TIA & Regards ...
Well, we were having this discussion earlier. Your template definition is sorta kinda code (it looks like code at any rate) but needs to be in the header and can't be in its own .cpp file (as it doesn't compile into anything).

I called it polymorphism but it seems it's a template.
In a way you are right, since templates provide a kind of "static polymorphism".

I suggest that you provide two versions of max, like what the standard library does for its max of two values:
Code:
```template<typename T>
inline const T& max(const T& a, const T& b, const T& c)
{
using std::max;
return max(a, max(b, c));
}

template<typename T, typename Compare>
inline const T& max(const T& a, const T& b, const T& c, Compare compare)
{
using std::max;
return max(a, max(b, c, compare), compare);
}```

6. I wanted to do this in order to clarify my previous C code when porting it to C++.

So, it's now in this way:
Code:
```template <class T>	T	max3bytemplate(T a, T b, T c);

//////////////////////////////////////////////////////////

template <class T>
T max3bytemplate(T a, T b, T c)
{
return ( max(a,max(b,c)) );
}

//////////////////////////////////////////////////////////```
I've added the prototype or declaration of the function however you call it, so I can put all the templates at the end of the header and understand better the code.

Is this the way it's normally do it, or is it done in another way that is even more clear?

TIA & Regards ...