How to find the name of the current function at runtime?

后端 未结 7 856
走了就别回头了
走了就别回头了 2021-01-31 15:17

After years of using the big ugly MFC ASSERT macro, I have finally decided to ditch it and create the ultimate ASSERT macro.

I am fine with getting the file and line num

7条回答
  •  感情败类
    2021-01-31 15:51

    Your macro can contain the __FUNCTION__ macro. Make no mistake, the function name will be inserted into the expanded code at compile time, but it will be the correct function name for each call to your macro. So it "seems like" it happens in run-time ;)

    e.g.

    #define THROW_IF(val) if (val) throw "error in " __FUNCTION__
    
    int foo()
    {
        int a = 0;
        THROW_IF(a > 0); // will throw "error in foo()"
    }
    

提交回复
热议问题