g++ __FUNCTION__ replace time(g++ __FUNCTION__ 替换时间)
问题描述
谁能知道 g++ 何时将 __FUNCTION__
'macro' 替换为包含函数名称的字符串?在检查源代码的语法正确性之前,它似乎可以替换它,即以下内容将不起作用
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 "
"); //*
}
自预处理后使用
g++ -E test.cc
来源看起来像
[...]
int main(int argc, char* argv)
{
printf(__FUNCTION__ "test.cc" "6" "
"); //*
}
现在编译器正确抛出,因为 *ed 行不正确.
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?
__FUNCTION__
真的被字符串替换了吗?还是编译后代码中的变量?
Is __FUNCTION__
really replaced with a string after all? Or is it a variable in the compiled code?
推荐答案
有什么方法可以强制将字符串替换到更早的步骤以使该行正确?
Is there any way to force that replacement with a string to an earlier step so that the line is correct?
没有.__FUNCTION__
(及其标准化对应物,__func__
)是 compiler 结构.另一方面,__FILE__
和 __LINE__
是 预处理器 结构.没有办法使 __FUNCTION__
成为预处理器构造,因为预处理器不了解 C++ 语言.当一个源文件被预处理时,预处理器完全不知道它正在查看哪个函数,因为它甚至没有函数的概念.
No. __FUNCTION__
(and its standardized counterpart, __func__
) are compiler constructs. __FILE__
and __LINE__
on the other hand, are preprocessor constructs. There is no way to make __FUNCTION__
a preprocessor construct because the preprocessor has no knowledge of the C++ language. When a source file is being preprocessed, the preprocessor has absolutely no idea about which function it is looking at because it doesn't even have a concept of functions.
另一方面,预处理器确实知道它正在处理哪个文件,并且它也知道它正在查看文件的哪一行,因此它能够处理 __FILE__
和 __LINE__
.
On the other hand, the preprocessor does know which file it is working on, and it also knows which line of the file it is looking at, so it is able to handle __FILE__
and __LINE__
.
这就是为什么 __func__
被定义为等同于静态局部变量(即 compiler 构造)的原因;只有编译器才能提供此功能.
This is why __func__
is defined as being equivalent to a static local variable (i.e. a compiler construct); only the compiler can provide this functionality.
这篇关于g++ __FUNCTION__ 替换时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:g++ __FUNCTION__ 替换时间
基础教程推荐
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01