Gettin line number of the called function

前端 未结 3 1686
长发绾君心
长发绾君心 2020-12-21 11:01

Please let me know if I can do it or not?

I am writing a library that could work to track memory allocation and de-allocation in C++. In short, I am trying to see if

3条回答
  •  时光取名叫无心
    2020-12-21 11:25

    Since C++20 you can use std::source_location which offers:

    • line
    • column
    • file_name
    • function_name

    For previous versions of C++, traditionnally the macros __LINE__ gives the line number but also __FUNCTION__ and __FILE__ are really helpful, giving the const char* of the enclosing function, and file name.

    Indeedn, __FUNCTION__ is not standard but supported by several compilers.

    Unfortunately these macros only take their value in place. So it is not possible for you to ask for the caller.

    You should write the __LINE__ and __FILE__ macros everywhere you use new. :(.

提交回复
热议问题