Minimum Macro

This is a discussion on Minimum Macro within the C++ Programming forums, part of the General Programming Boards category; I am trying setup a simple macro that compares 2 numbers and selects the lowest number. I found the following ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    Minimum Macro

    I am trying setup a simple macro that compares 2 numbers and selects the lowest number.

    I found the following example from someelse on the C++ board.

    #define MINIMUM1 (a, b) (((a) < (b)) ? (a) : (b))

    I am confused how I execute the macro from main. I have tried
    Code:
     
       double lowestNumber;
      
       lowestNumber = MINIMUM1(10, 5);
       cout << "Lowest number = " << lowestNumber;
    but that didn't even compile. I received the this error, "term does not evaluate to a function."

    Any help would be appreciated.

    Thanks,

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    #define MINIMUM1(a, b) (((a) < (b)) ? (a) : (b))
                   ^^^^ no space here

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >#define MINIMUM1 (a, b) (((a) < (b)) ? (a) : (b))
    Note the space between the macro name and the argument list. This shouldn't be there:
    Code:
    #define MINIMUM1(a, b) (((a) < (b)) ? (a) : (b))
    Of course, in C++ you would be better served with an inline template function:
    Code:
    template <typename T>
    T minimum1(T a, T b) {
      return (a < b) ? a : b;
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Thanks - I took the extra space out but am still getting the same compiler error.

    Any suggestions?

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The macro must appear in the same file, either directly or in an #include, before any use. "does not evaluate to a function" most likely means that the compiler sees symbol(whatever) before symbol is introduced. All the cool kids use std::min anyway. What happens with MINIMUM1(*p1++,*p2++);?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What happens with MINIMUM1(*p1++,*p2++);?
    You get what you deserve.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311

    Wink

    Actually I was thinking about this, it does run twice as fast We also have a very good chance of "thinking outside the box"

    edit: The above was written by marketing people, the actual speed improvement is only 50%
    Last edited by grib; 09-10-2003 at 10:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Minimum Positive Subsequence Sum
    By CrazyNorman in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2008, 04:25 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  5. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM

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