Search code examples
c++functionoverloadingprecompiler

Preprocessor macro overriding function definition in c++


I am fairly familiar with the basics of C++, but lack experience (mainly code in Java), so slightly "dumbed down" replies would be appreciated :)

I am extending a larger open source project, which uses a standard visual studio class limits.h, where the following code can be found:

template<> class numeric_limits<double>
    : public _Num_float_base
    {   // limits for type double
public:
    typedef double _Ty;

    static _Ty (max)() _THROW0()
    {   // return maximum value
        return (DBL_MAX);
    }

I have now imported another open source project, which uses minwindef.h which has this piece of code in it:

#ifndef max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
#endif

The build now breaks because for this line

SUMOReal distance = std::numeric_limits<SUMOReal>::max();

the compiler complains about max() being used without any parameters. Is there any quick way to get around this issue, or can I simply not use the library I imported? :/

Hope this was clear enough, thanks for any suggestions!!


Solution

  • In your compiler settings, have NOMINMAX be defined. This will stop the Windows headers from trying to define the min and max macros. This is the correct way to handle this issue; trying to #undef it is unreliable and error-prone. Search for NOMINMAX for more information on this flag.

    You can also do this in a pinch, but don't make it a habit:

    SUMOReal distance = (std::numeric_limits<SUMOReal>::max)();