Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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)?

like image 976
Thomas Matthews Avatar asked Jun 26 '12 19:06

Thomas Matthews


People also ask

What is the function of the __ inline?

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.

What is __ LINE __ in C?

__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.

What are inline functions give an example?

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.


2 Answers

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.

like image 55
Eric Finn Avatar answered Sep 19 '22 11:09

Eric Finn


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.

like image 34
mfontanini Avatar answered Sep 23 '22 11:09

mfontanini