In C/C++, is there a macro that would tell me a line number relative to the beginning of the function in which the macro is found, rather than relative to the beginning of the file?
In C/C++, is there a macro that would tell me a line number relative to the beginning of the function in which the macro is found, rather than relative to the beginning of the file?
No, but you can do something somewhat equivalent by creating a line offset:
int func(char *s)
{ static const int begin_line = __LINE__;
#define func_line (__LINE__ - begin_line)
/* ... */
printf("%s():%d\n", __func__, func_line);
}
Output:
func():3
Note that begin_line being placed on the same line as the bracket is important. If you want zero-based line indexes then feel free to add a newline.
In C++ you can use constexpr instead of static const for compile-time evaluation of begin_line.