Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The same name macro with different arguments

Is it possible to have 2 macros with the same name, but different arguments? Something like this:

#define FI(value) do {l<<value;  Doit(l); } while(0)
#define FI(value, level) do {l<<value ; Doit(l,level); } while(0)
like image 551
vico Avatar asked Jan 02 '13 14:01

vico


People also ask

What is the correct format for naming a macro with multiple arguments?

Macros with arguments To create a macro with arguments, put them in parentheses separated by commas after the macro name, e.g. then BadSquare(3+4) would give 3+4*3+4, which evaluates to 19, which is probably not what we intended.

How many arguments macro can have?

For portability, you should not have more than 31 parameters for a macro. The parameter list may end with an ellipsis (…).

How can we define macro with different values?

The process to redefine a Macro is: Macro must be defined. When, you want to redefine the Macro, first of all, undefined the Macro by using #undef preprocessor directive. And, then define the Macro again by using #define preprocessor directive.

What is macro explain macro with arguments?

macro (array[x = y, x + 1]) passes two arguments to macro : array[x = y and x + 1] . If you want to supply array[x = y, x + 1] as an argument, you can write it as array[(x = y, x + 1)] , which is equivalent C code. All arguments to a macro are completely macro-expanded before they are substituted into the macro body.


1 Answers

It is not possible.
A symbol name cannot be redefined. Unlike functions macros cannot be overloaded. Think of it logically macros are for pure textual replacement, So how can you replace two different things for the same entity?

An alternative and better solution:
You can write a inline function for achieving the same result. It provides you additional advantage of type checking and saves you from the murky side effects of macros.

like image 153
Alok Save Avatar answered Sep 22 '22 10:09

Alok Save