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?
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)?
The __inline keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not __inline is honored depends on the setting of the inline optimizer option. By default, the inline option is in effect whenever the optimizer is run.
__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.
Example. In the following class declaration, the Account constructor is an inline function. The member functions GetBalance , Deposit , and Withdraw aren't specified as inline but can be implemented as inline functions. In the class declaration, the functions were declared without the inline keyword.
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.
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. Inlining will not affect this behaviour at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With