g++ __FUNCTION__ 替换时间

g++ __FUNCTION__ replace time(g++ __FUNCTION__ 替换时间)

本文介绍了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__ 替换时间

基础教程推荐