min and max functions on linux
on the windows version of my project i am using min, and max. Now when i try to build on linux it says they are not defined.
alternatively you could define them yourself
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
There are reasons why macros are generally considered evil:
Just use the <algorithm> header.
Enigma
#include <algorithm>#include <iostream>int main(){ int a = 8; int b = 9; std::cout << std::min(a++, b) << '\n'; std::cout << std::max(++a, --b) << '\n'; a = 8; b = 9;#define min(a, b) (((a) < (b)) ? (a) : (b))#define max(a, b) (((a) > (b)) ? (a) : (b)) std::cout << min(a++, b) << '\n'; std::cout << max(++a, --b) << '\n';}
Just use the <algorithm> header.
Enigma
Under Linux, chances are you're using GCC, which doesn't use microsoft's (and/or intels) extentions.
I believe that min and max are defined (as macros) in Windows.h or one of the files it includes.
Because they are macros you cannot use std::min and std::max when min and max macros are defined.
If you define the symbol NOMINMAX before including windows.h the macros will not be defined, so you can then use std::max and std::min, which, being part of the standard library, are portable and available on both linux and Windows.
See this
Because they are macros you cannot use std::min and std::max when min and max macros are defined.
If you define the symbol NOMINMAX before including windows.h the macros will not be defined, so you can then use std::max and std::min, which, being part of the standard library, are portable and available on both linux and Windows.
See this
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement