# How to define a polymorphic function for numeric values

• 02-21-2009
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 ...
• 02-21-2009
tabstop
What built-in function __max?

Anyway, I can't see any polymorphism here, but I do see templates.
• 02-21-2009
Quote:

Originally Posted by tabstop
What built-in function __max?

The one in Visual Studio C++, defined in stdlib.h
Quote:

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 ...
• 02-21-2009
tabstop
Quote:

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.
Quote:

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).
• 02-22-2009
laserlight
Quote:

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); }```
• 02-22-2009
```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)) ); } //////////////////////////////////////////////////////////```