Visual C++ equivalent of __FILE__ , __LINE__ and __PRETTY_FUNCTION__

后端 未结 8 2269
后悔当初
后悔当初 2020-12-14 08:50

GCC compiler gives me the following macros:

  • __FILE__ so that I can print out the file name + directory.
  • __LINE__ so that I c
8条回答
  •  清歌不尽
    2020-12-14 09:16

    Using c++14 with constexpr you can use this: WHERE macro.

    Based on usage of:

    • __PRETTY_FUNCTION__
    • __LINE__
    #include "string/ConstexprString.hpp"
    
    #define S1(x) #x
    #define S2(x) S1(x)
    
    // WHERE - const char* const should be used as temporary value
    #define WHERE (string::make(__PRETTY_FUNCTION__) + ":" + string::make(S2(__LINE__))).get()
    
    // It is safe to store e.g. `constexpr auto where = WHERE_STR;`
    #define WHERE_STR (string::make(__PRETTY_FUNCTION__) + ":" + string::make(S2(__LINE__)))
    

    Example usage:

        // Called: void (anonymous namespace)::exampleUseCaseWhere(int):18
        std::cout << "Called: " << WHERE << std::endl;
    

    Full & running example here

    See:

    • src/acme/where.hpp
    • src/string/ConstexprString.hpp
    • src/acme/where_test.cpp

提交回复
热议问题