Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ #define variable parameter function

I have a CPU sensitive application and want to minimize function calls. I want to write something like:

#ifdef condition        
#define f(a,b) ff(a,b)   
#define f(a) ff(a)    
#endif    

But the compiler sees f as defined multiple times. I wanted to use __VAR_ARGS__ but in the example above b is of enum type. Is there a proper way to do it or I should just rename f(a,b) to f2(a,b)?

To clarify the defines, if active, add calls to functions that process data for printing to file/stdout, otherwise they are replaced with empty lines, so in my opinion this method would improve code size and since the macro is single line keyword like INFO(object->contents) I think it's more readable. Also it would have been useful if I could have added something like WARN("message") and WARN("value is",obj->printvalue()).

I also think inline would do the trick (from the answer below).

like image 614
LucianMLI Avatar asked Nov 17 '25 10:11

LucianMLI


1 Answers

This is a very C-ish way of approaching this. Simply make it an overloaded inline function. Any optimiser worthy of the name will inline the call.

like image 127
Angew is no longer proud of SO Avatar answered Nov 19 '25 00:11

Angew is no longer proud of SO