简体   繁体   English

如何在编译时查找函数是否为静态

[英]How to find if function is static at compile time

I have logging macro which logs the this pointer of the function but the problem is when logging macro used in static function which doesn't have this pointer and I get compilation error. 我有记录该函数的this指针的日志记录宏,但是问题是当记录静态函数中使用的没有此指针的宏时,出现编译错误。

eg 例如

LOG_DEBUG(msg,...) \
   Log::WriteLog(Log::LOG_DEBUG, __FILE__, __LINE__, _FUNC_NAME_, this, msg, ## __VA_ARGS__);

I was wondering if there was a way to check if the current method is static than I can either use this pointer or pass null and avoid compilation error. 我想知道是否有一种方法可以检查当前方法是否是静态的,所以我可以使用此指针或传递null并避免编译错误。

Please let me know if GNU provide any predefine macros to determine if current function is static or any alternate way. 请让我知道GNU是否提供任何预定义宏来确定当前函数是静态的还是任何替代方式。

I would avoid trying to auto detect and make it the responsibility of the developer. 我会避免尝试自动检测并使其成为开发人员的责任。
Just provide another logging macros for situations where this is no this value. 仅在没有此值的情况下提供另一个日志记录宏。
The compiler will generate an error when you use LOG_DEBUG() incorrectly and you can replace. 当您错误地使用LOG_DEBUG()并可以替换时,编译器将生成一个错误。

LOG_NON_OO_DEBUG(msg,...) \
   Log::WriteLog(Log::LOG_DEBUG, __FILE__, __LINE__, _FUNC_NAME_, msg, ## __VA_ARGS__);

Note: I would not pass NULL (or 0 either). 注意:我不会传递NULL(或0)。
I would overload the WriteLog() method so that it has a version that does not require a this pointer. 我将重载WriteLog()方法,使其具有不需要this指针的版本。

Adding to Martin's answer, this is a compile-time construct, whereas macros are pre-processor, so there's no way to test for the existence of this with a macro. 添加到马丁的答案, 是一个编译时的结构,而宏预处理器内,所以也没有办法来测试这个存在了宏。 In less strongly typed languages like perl you can check whether a symbol is defined, but since C/C++ are strongly typed there's no need for such an operator. 在不太强类型的语言(如perl)中,您可以检查是否定义了符号,但是由于C / C ++是强类型的,因此不需要这种运算符。

edit 编辑

I want to clarify the point I was trying to make in the above paragraph a bit. 我想澄清一下我在上一段中试图指出的观点。

The pre-processor is a Cism. 预处理器是Cism。 If I had to bet, it probably has seen only bug fixes and minor changes to its behavior but otherwise is nearly identical in behavior to C from 20 years ago. 如果我必须打赌,它可能只看到了错误修复,并且对其行为进行了微小的更改,但是从20年前开始,其行为与C几乎相同。 In other words, the pre-processor isn't designed to work on language-specific features. 换句话说,预处理器并非设计用于特定于语言的功能。

Language features like static , this , calling conventions, types, etc ... the pre-processor isn't aware of them, and doesn't have logic for working with them. 语言功能(例如staticthis ,调用约定,类型等)……预处理器不了解它们,也没有使用它们的逻辑。 The only way this could be pulled off is if a language feature exists to test the definedness of this , or test to see whether you're in a static function or not. 这可能是被拉断的唯一方法是,如果一种语言特性的存在是为了测试这个 definedness,或进行测试,看是否是在一个静态函数或没有。

/edit /编辑

I'm inclined to suggest you use a non-macro approach to logging, or a less macro-dependent solution. 我倾向于建议您使用非宏方法进行日志记录,或者使用较少依赖宏的解决方案。 The only advantage I see to using macros is you don't have to type out __FILE__ and __LINE__ all over the place. 我看到使用宏的唯一好处是您不必在所有地方键入__FILE____LINE__ In that respect I don't see an issue with wrapping whatever you come up with in a macro. 在这方面,我看不到将宏中的任何内容包装起来的问题。

For example, you could add a generic function to your classes that generates a terse piece of information, something like: 例如,您可以在类中添加一个泛型函数,以生成简短的信息,例如:

class MyClass {
  private:
    void MemberLogMsg();
  public:
    static void LogMsg( MyClass* M = NULL);
};

Your macro would always call LogMsg, and as Martin suggested the onus would be on the developer to pass in this or not. 您的宏将始终调用LogMsg,并且正如Martin所建议的那样,开发人员可以自行决定是否传递此信息

Now, I won't stand by the quality of the above example, only by the spirit of what it's meant to express. 现在,我不会仅仅依靠上面所表达的精神来支持上面示例的质量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM