Visual C++ equivalent of __FILE__ , __LINE__ and __PRETTY_FUNCTION__

后端 未结 8 2228
后悔当初
后悔当初 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
    0 讨论(0)
  • 2020-12-14 09:19

    __FILE__ and __LINE__ are standard, and I'm rather certain Microsoft compilers have essentially always had them.

    __PRETTY_FUNCTION__ is a gcc feature.

    0 讨论(0)
  • 2020-12-14 09:20

    In VS2008, this:

    struct A
    {
        bool Test(int iDummyArg)
        {
            const char *szFile = __FILE__;
            int iLine = __LINE__;
            const char *szFunc = __FUNCTION__; // Func name
            const char *szFunD = __FUNCDNAME__; // Decorated
            const char *szFunS = __FUNCSIG__; // Signature
    
            printf("%s\n", szFile);
            printf("%d\n", iLine);
            printf("%s\n", szFunc);
            printf("%s\n", szFunD);
            printf("%s\n", szFunS);
    
            return true;
        }
    };
    
    int wmain(int argc, TCHAR *lpszArgv[])
    {
        A a;
        a.Test(10);
    }
    

    will print this:

    c:\source\test_projects\blah\blah.cpp
    14
    A::Test
    ?Test@A@@QAE_NH@Z
    bool __thiscall A::Test(int)
    

    (The line number is "wrong" since there was really some extra stuff at the top of my file.)

    0 讨论(0)
  • 2020-12-14 09:21

    I know that MSVC offers __FILE__ and __LINE__, both of which are Standard macros. They also offer __FUNCTION__, which I believe is what you're looking for,

    0 讨论(0)
  • 2020-12-14 09:22

    For more portability in getting the current function name, you can try BOOST_CURRENT_FUNCTION.

    0 讨论(0)
  • 2020-12-14 09:25
    1. Yes, Microsoft Visual Studio has __FILE__ and __LINE__. Here are more MSVC macros.

    2. Both are ANSI C++.

    3. MSVC has __FUNCTION__, which is Microsoft-specific.

    0 讨论(0)
提交回复
热议问题