Sign in to follow this  
Mushu

SOLVED std::max() workaround

Recommended Posts

Yes, I've been trying to break my VB whoring ways (reformatting the HD helped a little) so I've been working through Accelerated C++ Problem is, one of the examples won't compile - it requires a call to std::max() which, apparently, doesn't exist. (And yes, I've defined <algorithm>, first thing I checked) So, I did a little googling, and found out that in MSVS6, the std::min() and std::max() functions were removed because they conflicted with <windows.h> So, found their workaround, _cpp_min() and _cpp_max(), but is there a workaround around their workaround? Can I overload the function with something similar to a typedef (templates, perhaps)? Wait... can you even overload a function with no parameters if the means of determining which version of an overloaded function to use IS the parameters? "No" would be my guess... am I right? EDIT: solved! Yay! [Edited by - Mushu on November 30, 2004 10:11:15 PM]

Share this post


Link to post
Share on other sites
To get around the windows.h nightmare, I do one of a few things:

1) Avoid including it at almost any cost
2) Failing that, I use #undef min and #undef max, and that should make std::min and std::max work a treat in that compilation unit.

Share this post


Link to post
Share on other sites
I don't include it. I think it's included in one of the other headers I put in there (or my compiler is evil).

I'll try the #undef, but I'm not entirely sure that will work - min() and max() don't appear to be members of std at all. I'll know in several minutes, regardless. Thanks

EDIT: nope; I was right. min() and max() are no longer members of the standard library.

Share this post


Link to post
Share on other sites
Defining std::min() and std::max() for yourself isn't that hard. Ex:

template <typename T> inline
const T & min(const T & left, const T & right) {
return (right < left ? right : left);
}

Share this post


Link to post
Share on other sites
Yeah, I had an inkling of a feeling there was a solution with templates, but those don't come up for another couple chapters.

...let me try that code anyway, after all you did take the time to type it for me [grin]

EDIT: Yep. That appears to have worked! I'll just switch the signs to get me max(), then... Oh, and if Washu is reading this I still despise templates, just so we can keep that straight.

EDIT2: std::_cpp_min() and std::_cpp_max() seem to work fine too. I wonder why I didn't notice those sooner? Oh well... Thanks everyone!

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Defining std::min() and std::max() for yourself isn't that hard. Ex:

template <typename T> inline
const T & min(const T & left, const T & right) {
return (right < left ? right : left);
}


Don't forget to do

#undef min
#undef max


Before you define your new template functions for min and max, otherwise your preprocessor will do some tomfoolery with the function names.

Share this post


Link to post
Share on other sites
Actually it works fine without it. The problem was std::min() and std::max() are just renamed in MSVC6; _technically_ it would be a problem if "windows.h" was included, but that wasn't where my problem was stemming from.

Just a stupid failure to recognize that std::_cpp_max() == std::max() - They just renamed the darn functions.

That done, I think I'm gonna hit the sack. Thanks for the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this