g ++ __FUNCTION__替换时间 [英] g++ __FUNCTION__ replace time

查看:190
本文介绍了g ++ __FUNCTION__替换时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉g ++用包含函数名的字符串替换 __ FUNCTION __ '宏?它似乎可以取代它,直到它检查的源代码的语法正确性,即以下将不工作

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"); //*
}

b $ b

g++ -E test.cc

源类似

[...]

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

现在编译器正确地抛出,因为* 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 __ )是编译器结构。另一方面, __ 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__.

c> __ func __ 定义为等价于静态局部变量(即编译器构造);只有编译器才能提供此功能。

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__替换时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆