How to define a polymorphic function for numeric values

This is a discussion on How to define a polymorphic function for numeric values within the C++ Programming forums, part of the General Programming Boards category; Hi, Sorry for this question if it's too basic but I'm just a C programmer starting with C++, I want ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    8

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What built-in function __max?

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

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    Quote Originally Posted by tabstop View Post
    What built-in function __max?
    The one in Visual Studio C++, defined in stdlib.h
    Quote Originally Posted by tabstop View Post
    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?

    // In the header file
    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 ...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Adrian20XX View Post
    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 Originally Posted by Adrian20XX View Post
    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?

    // In the header file
    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).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,767
    Quote Originally Posted by Adrian20XX
    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);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    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 ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing flags from a DLL?
    By RobotGymnast in forum C++ Programming
    Replies: 17
    Last Post: 10-27-2008, 01:34 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21