Advertisement

Overloading macros - Can anyone do it??

Started by December 07, 2000 10:23 PM
10 comments, last by Void 24 years, 1 month ago
Does anyone have any "tricks" that can enable a macro to be overloaded like so?
  
#define MacroName(x)  ..

#define MacroName(x, y) ..
  
I forgot to add.. I cannot #define the macro name to be a name of a overloaded function because I need to paste the __FILE__, __LINE__ macro in the caller

ie. can''t do this

  void FunctionName(int x);void FunctionName(int x, int y);#define MACRO  Function_NameMACRO(10);MACRO(10, 20);  

Advertisement
Short answer: Can''t be done. And they can''t be recursive, either.

Use instead, for example, function with varargs (similar to printf), or inline functions pretending to be macros.
~~~ "'impossible' is a word in the dictonary of fools" --Napoleon
Definitely use inline functions for this. There is no reason not too, in this case.

inline MacroName(int x) { // do whatever; }
inline MacroName(int x, int y) { // do whatever; }

What''s cool is that now you will get type-checking when compiling.

Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
quote: Original post by Dire.Wolf
Definitely use inline functions for this. There is no reason not too, in this case.


Can''t use inlines because I need to paste the __FILE__ and __LINE__ macro from the caller. If I use an inline function, the macros point to the inline function, not the caller.

What I want to do is to replace the assert macro but "overload" it to take different # of arguments.

If you want this for an assert that takes a description and one that doesn''t, you can do this:

assert(Condition && "Description");
Using the same assert that would do this:
assert(Condition);

I think this works..
Advertisement
That is still the 1 parameter passed to the macro..I want a macro that can take a variable # of arguments..

I guess it can''t be done..
Manual name mangling

#define 1argMacroName(x)
#define 2argMacroName(x, y)

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote: Original post by Magmai Kai Holmlor

Manual name mangling

#define 1argMacroName(x)
#define 2argMacroName(x, y)


That''s what I don''t want to do..overlitter it with macros.

Well if you were using GNU-C (ie. PSX-1 programming) you could use one of its extensions...

#define Macro(a,b...) printf (a,b)

and it would do just want you want...

But under ANSI-C/C++ you can''t

This topic is closed to new replies.

Advertisement