我可以存储指向 __PRETTY_FUNCTION__ 的指针供以后使用,还是需要立即复制该字符串?

Can I store pointer to __PRETTY_FUNCTION__ for later use or do I need to copy the string immediately?

__PRETTY_FUNCTION__ 产生的字符串的生命周期是多少?

我知道 __func__ 生成等同于 static const char __func__[] = "function-name";,多亏了那个 static,如果我想在某个地方创建函数中发生的事情的记录以供以后引用,我可以只将 __func__ 的值存储在记录的 const char* 字段中,并在我离开函数范围很久之后随时检索它。

__PRETTY_FUNCTION__ 的行为方式是否相同,或者它生成的字符串是否表现为本地临时值?说,这会可靠地工作,还是会失败,fname 指向函数名称曾经所在的位置,我需要做一个缓冲区并做 strncpy() 或类似的代替?

#include <stdio.h>
const char* fname = NULL;

void foo()
{
    fname = __PRETTY_FUNCTION__;
}

int main()
{
    foo();
    printf(fname);
    return 0;
}

所有三个常数 __func____FUNCTION____PRETTY_FUNCTION__ 的行为方式相同。 GCC Manual 状态:

GCC provides three magic constants that hold the name of the current function as a string. In C++11 and later modes, all three are treated as constant expressions and can be used in constexpr contexts.