tags:

views:

270

answers:

6

Hi, I use one library which is very important and untouchable. The problem is that library declare min, max function, so when I include STL header in project, they conflict. I want to disable min, max function in STL (like #define NOMNMAX) if I could. If I can't, what would be solution?

Important : Sorry, It's not Macro. Two functions are template functions.

like

template<T>
T min(const T& a, const T& b) { a < b ? a : b; }

Thanks, in advance.

+17  A: 

The min and max functions are defined in the std namespace, so this code should not compile:

#include <algorithm>

int main() {
    int n = min( 1, 2 );
}

If it does, your Standard Library is non-compliant. Also, your important and untouchable library should be declaring it's functions in a namespace. If it isn't. complain loudly to the vendor.

Edit: As these functions are presumably in a header file, you can touch them. So a hack would be to remove the templates from the header and replace them with:

using std::nin;
using std::max;

though why the writers of the library felt the need to define these templates is anyone's guess.

anon
I'm guessing the library is declaring min/max as macros, so namespaces might not help
nikie
anon
Another possibility is that the OP has a `using namespace std` somewhere.
jalf
@Jalf Surely nobody would do that!!!
anon
Surely no one would #define min and max either. And no one would want to disable `std::min`/`std::max` when a conflict arises because of it. If only we could rely on developers to do the right thing. ;)
jalf
chrispy
+6  A: 

As both min and max (and every other standard library member) are defined in std namespace, you just mustn't import that namespace, i.e. don't use using namespace std;. You can still use STL, by explicit namespace resultion, eg. std::max, std::cout etc.

el.pescado
Wouldn't matter, would it? You don't cause a conflict by merely writing `using namespace std;`.
MSalters
+2  A: 

I sometimes have problems with something like that as well, because iirc OpenCV #defines its own min/max, as well as i think does windows.h. Usually I help myself by simply

#undef min
#undef max

Neil is right, though, as min is in std:: namespace. For me the problem arises, that with some headers #defining min/max, i can't even use std::min()

zerm
A: 

I'm guessing your libary defines min/max as macros, so the fact that STL min/min are in a namespace won't help. You could try this:

#define max STL_MAX
#define min STL_MIN
#include <algorithm>
#undef min
#undef max

That way, min and max are translated to something else while algorithm is compiled. Of course this only works if you include algorithm before your library.

nikie
Why does the STL namespace not help? "max()" will never call the STL max() (unless one uses using namespace std;, which is a bad idea anyway).
Frank
@Frank: the preprocessor ignores namespaces. So if you include a header that `#define`s min/max, then include STL, the preprocessor will replace min/max in the STL header, and the STL header won't compile.
nikie
+2  A: 

If your untouchable library is not in a namespace, you could force the lookup to use global scope, rather than std:

int i = ::min<int>(1,2);

A better solution would be to remove

using namespace std

and get your library in a namespace.

DanDan
A: 

Define NOMINMAX, and you won't get either of these functions.

#define NOMINMAX
Matt Joiner
Assuming his vendor provides such functionality. He never said it was Windows, which is where `NOMINMAX` is generally used. (In fact, he said he wanted something like it, implying it doesn't exist.)
GMan
Yeah he didn't mention, I don't really use C++ on Linux, but I used to define this macro all the time on Windoze.
Matt Joiner