implementation safe nullptr

前端 未结 3 989
余生分开走
余生分开走 2021-01-01 00:14

I\'d like to keep my code compilable both on legacy C++ (C++ code using \"NULL\") and new C++11 standard (C++ code using \"nullptr\")

I\'m using GCC, but planning to

3条回答
  •  一生所求
    2021-01-01 00:57

    I think following will works:

    #include 
    
    #ifndef MY_LIB_NULL
        #ifndef NULL //check for NULL
            #define MY_LIB_NULL nullptr
        #else
            #define MY_LIB_NULL NULL ///use NULL if present
        #endif
    #endif
    

    basically I check for "NULL". wich is a macro and can be checked, until the compiler is shipped with that macro (likely to be), than it's valid using the macro, when compiler will only provides "nullptr" and no longer have NULL then nullptr is used (maybe in a far future, but seems we can happily continue to use NULL!)

    I think that's safer than redefining "nullptr" (like most people trying to do)

提交回复
热议问题