我可以将 __func__ 替换为 C 宏中的标识符名称吗? [英] Can I substitute __func__ into an identifier name in a C macro?

查看:11
本文介绍了我可以将 __func__ 替换为 C 宏中的标识符名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个 C 宏来接受这个:

I'd like to write a C macro which takes this:

int foo() {
  MY_MACRO
}

并将其扩展为:

int foo() {
  _macro_var_foo++;
}

我发现我不能使用 __func__,因为它实际上并没有在宏中得到扩展;它被预处理器视为变量.

I've found that I can't use __func__, because that doesn't actually get expanded in the macro; it's treated by the preprocessor like a variable.

有什么方法可以让它工作吗?

Is there some way to get this to work?

推荐答案

预处理器不知道函数,只知道源文件和行号.在那个阶段,它不执行句法分析,只是进行文本分析和替换.这就是为什么 __func__ 是一个神奇的变量,而不是像 __FILE____LINE__ 这样的神奇宏.

The preprocessor doesn't know about functions, just source files and line numbers. At that stage it's not performing syntactical analysis, just textual analysis and substitutions. That's why __func__ is a magical variable instead of a magical macro like __FILE__ and __LINE__.

这篇关于我可以将 __func__ 替换为 C 宏中的标识符名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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