我完全可以替代__func__成一个标识符名称在C宏? [英] Can I substitute __func__ into an identifier name in a C macro?

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

问题描述

我想写一个C宏这需要这样的:

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

int foo() {
  MY_MACRO
}

和它扩展到这一点:

int foo() {
  _macro_var_foo++;
}

我发现我不能使用 __ __ FUNC ,因为这实际上并没有得到在宏扩大;它是由preprocessor像一个变量处理。

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?

推荐答案

在preprocessor不知道的功能,只是源文件和行号。在这个阶段它不执行语法分析,只是文本分析和替换。这就是为什么 __ 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天全站免登陆