[英]Behavior of __LINE__ in inline functions
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__);
How will the __LINE__
be resolved when used inside an inlined function? 在内联函数中使用时,如何解析__LINE__
?
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;
}
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)? __LINE__
包含头文件中的行号,或者调用内联函数的源文件的行号(假设编译器在源代码中执行“粘贴”)?
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"). 在C和C ++中,宏(大多数情况下)不是用实际代码的任何知识进行评估,而是在代码之前处理(因此称为“预处理器”)。 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. 因此, __FILE__
将计算为“file.h”,并且__LINE__
将计算为与file.h中出现SYSTEM_FAILURE
的行对应的行号。
Since macros are replaced by their definitions before compilation, the __LINE__
will contain the actual line of the file in which you used the macro. 由于宏在编译之前被其定义替换,因此__LINE__
将包含您在其中使用宏的文件的实际行。 Inlining will not affect this behaviour at all. 内联不会影响这种行为。
__LINE__
将是头文件的行,因为预处理器将在编译器看到它之前对其进行评估。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.