g++ __FUNCTION__ replace time

2019-01-27 20:11发布

Can anyone tell when g++ replaces the __FUNCTION__ 'macro' with the string containing the function name? It seems it can replace it not until it has check the syntactical correctness of the source code, i.e. the following will not work

#include <whatsneeded>
#define DBG_WHEREAMI __FUNCTION__ __FILE__ __LINE__

int main(int argc, char* argv)
{
  printf(DBG_WHEREAMI "\n"); //*
}

since after preprocessing using

g++ -E test.cc

the source looks like

[...]

int main(int argc, char* argv)
{
  printf(__FUNCTION__ "test.cc" "6" "\n"); //*
}

and now the compiler rightly throws up because the *ed line is incorrect.

Is there any way to force that replacement with a string to an earlier step so that the line is correct?

Is __FUNCTION__ really replaced with a string after all? Or is it a variable in the compiled code?

7条回答
欢心
2楼-- · 2019-01-27 21:14

__FUNCTION__ is not standard. Use __func__. As the documentation says, it's as if:

<ret-type> function_name( <args> )
{
    static const char __func__[] = "function-name";
    ...
查看更多
登录 后发表回答