Token pasting and __LINE__

前端 未结 2 833
我寻月下人不归
我寻月下人不归 2020-12-19 15:20

I\'m writing a simple macro to show TRACE information.

This is what I\'m using ,

#ifdef __DEBUG__
#define TRACE  { PrintErrorMsg(\"Trace exception at         


        
2条回答
  •  旧巷少年郎
    2020-12-19 15:34

    You need this kind of silliness, unfortunately.

    #include 
    
    #define TRACE2(f,l) printf("I am at file: " f " and line: " #l "\n")
    #define TRACE1(f,l) TRACE2(f,l)
    #define TRACE() TRACE1(__FILE__, __LINE__)
    
    int main(void)
    {
        TRACE();
        TRACE();
    }
    

    I am at file: test.cpp and line: 9
    I am at file: test.cpp and line: 10

提交回复
热议问题