0

I have a header file for definitions . I want to have a max and min function for each built-in type .

I use the following macro :

#define DEFINE__MIN_MAX(type) \
   inline type max(type x, type y) { return (x>y) ?  x : y ; } \
   inline type min(type x, type y) { return (x<y) ?  x : y ; }

Now I call the macro to specialize short data-type

DEFINE_MIN_MAX(short)  // Error: type 'short' unexpected .

I am trying this in Windows using QtCreator 3.0.1 . I am not sure how to deal with this error.

Any inputs are welcome .

user3389943
  • 147
  • 1
  • 7
  • 2
    You're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). These already exist in `` as templates. – chris Apr 16 '14 at 19:36
  • 3
    Is there a reason you don't toss the macros and just use a (pair of) template function(s)? – mah Apr 16 '14 at 19:37
  • You effectively accomplished a function template with extra work. mah is right, you should just write a function template. – chbaker0 Apr 16 '14 at 19:51
  • @chris : I have not included . It should not recognize max and min as already defined , ins't it ? – user3389943 Apr 16 '14 at 19:58
  • @user3389943, Some other header could still include it. Anyway, my point was to use the ones in there instead of reinventing the wheel. – chris Apr 16 '14 at 20:01
  • Also `windows.h` has these identifiers defined, albeit in a very foolish way – Mooing Duck Apr 16 '14 at 22:28

2 Answers2

1

Trivial error: you have a double underscore in the definition, but use only one underscore in the call. Change the line

#define DEFINE__MIN_MAX(type) \

to

#define DEFINE_MIN_MAX(type) \

and it should work fine.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
1

Stay away from macros. C++ provides type-safe replacements for almost all standard usages of macros. For your case, where you want a min and max function for every type T, express this as a template:

template<typename T> T const& min(T const& x, T const& y) {
    return (x < y) ? (x) : (y);
}

template<typename T> T const& max(T const& x, T const& y) {
    return (x > y) ? (x) : (y);
}

Or use std::min and std::max from the standard library.

Jens
  • 9,058
  • 2
  • 26
  • 43
  • Nice. This enables moving in cases like `Int useMoveContructor(min(Int(1), Int(2)));` if Int has a move constructor, right? I wonder why even in C++14 std::min and std::max are still defined as in my answer. Backwards compatibility? – Jens Apr 16 '14 at 22:38
  • wait, I just realized my code was wrong, and wouldn't compile since it could not determine the return type at compile time >. – Mooing Duck Apr 16 '14 at 22:44
  • Still I think it would make sense to define the functions like `template T&& min(T&& x, T&& y) {return (x < y) ? (std::forward(x)) : (std::forward(y));}` on C++11. Any idea why std::min is not defined that way? – Jens Apr 17 '14 at 06:52
  • backwards compatibility I'd assume, though I'm not sure anyone can say for certain – Mooing Duck Apr 17 '14 at 16:19