Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicitly pass arguments

Tags:

c

I'm working on a custom unit-testing framework that doesn't display the filename/line numbers of failed tests. I know I could require them as arguments for the report_results function but that seems like a lot of text.

Is there a way to make it so that __FILE__ and __LINE__ are always passed with the function, even if I don't pass them when I call it?

You can do something like this in C++:

void func(char *file=__FILE__, int line=__LINE__)(int more_args) {}
like image 756
Coolcodehere Avatar asked Jan 22 '26 22:01

Coolcodehere


1 Answers

use a macro in your code, to call an underlying function

#define func(arg) real_func(__FILE__,__LINE__,arg)

where real_func is

void real_func(const char *file, int line, int arg);

In your code, when you call func(12), it expands to a call to real_func, with actual file path and line of the call. You can even support variable argument functions in C99 with variadic macros as a frontend.

like image 156
Jean-François Fabre Avatar answered Jan 25 '26 14:01

Jean-François Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!