在inline函数__LINE__行为 [英] Behavior of __LINE__ in inline functions

查看:226
本文介绍了在inline函数__LINE__行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有传递行号和文件名,以一个错误处理的宏

I have a macro that passes the line number and file name to an error handler:

#define SYSTEM_FAILURE (error_code, comment) \
   System_Failure((error_code), (comment), __LINE__, __FILE__);

如何将 __ __ LINE 内联函数内使用时可以解决?

How will the __LINE__ be resolved when used inside an inlined function?

file.h:
inline int divide(int x, int y)
{
    if (y == 0)
    {
        SYSTEM_FAILURE(ENUM_DIVIDE_BY_ZERO, "divide by zero error");
    }
    return x/y;
}

威尔 __ LINE __ 包含头文件中的行号,或源文件的行数,其中内联函数被调用(假设编译器一个粘贴源$ C ​​$ C)?

Will __LINE__ contain the line number within the header file, or the line number of the source file where the inline function is called (assuming compiler does a "paste" in the source code)?

推荐答案

在C和C ++中,宏都没有(大部分)与实际code的任何知识评估和$ C之前被处理$ C(因此命名为preprocessor)。因此, __ FILE __ 将评估为file.h,而 __ LINE __ 将评估为对应于行的行号在其 SYSTEM_FAILURE 出现在file.h。

In C and C++, macros aren't (for the most part) evaluated with any knowledge of the actual code and are processed before the code (hence the name "preprocessor"). Therefore, __FILE__ would evaluate to "file.h", and __LINE__ would evaluate to the line number corresponding to the line on which SYSTEM_FAILURE appears in file.h.

这篇关于在inline函数__LINE__行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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