How to call std::min() when min has been defined as a macro?

后端 未结 5 1845
误落风尘
误落风尘 2020-12-15 15:54

How do I call std::min when min has already been defined as a macro?

5条回答
  •  执念已碎
    2020-12-15 16:17

    You might be able to avoid the macro definition by:

    • #undef
    • avoid the definition in the first place (either by configuration such as #define NOMINMAX or similar or avoiding including the offending header)

    If those options can't be used or you don't want to use them, you can always avoid invoking a function-like macro with an appropriate use of parens:

    #include 
    #include 
    
    #define min(x,y) (((x) < (y)) ? (x) : (y))
    
    int main() 
    {
        printf( "min is %d\n", (std::min)( 3, 5));  // note: the macro version of `min` is avoided
    }
    

    This is portable and has worked since the dark, early days of C.

提交回复
热议问题