
Question:
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.
Solution:1
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 the following:
using std::min; using std::max;
though why the writers of the library felt the need to define these templates is anyone's guess.
Solution:2
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.
Solution:3
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.
Solution:4
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()
Solution:5
Define NOMINMAX
, and you won't get either of these functions.
#define NOMINMAX
Solution:6
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.
Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
EmoticonEmoticon