-1

I'm trying to do something like the following:

//Bad; can't redefine macros; uses later definition.
#define foo          )
#define foo(arg) ,arg)

That is, I want foo (note: not a macro function) to map to one thing, and I want the macro function foo(arg) to map to something else. So foo needs to be some #define constant (catching both cases) that maps onto . . . something.


I haven't been able to figure out a way, (and since this is a macro and a constant, the many previous questions do not apply). How can I do this?

Evil compiler-/platform-specific options are great too. Tagging this for C-macros, although I'm using C++14.


Sidenote (by request): this could be used for e.g. making your own debug overloads for new that would work with placement new as well:

#define new new(__FILE__,__LINE__ foo
//...
void const* p1 = new       int();
void const* p2 = new (ptr) int();
Community
  • 1
  • 1
geometrian
  • 14,775
  • 10
  • 56
  • 132
  • 2
    Can you describe a reason for such a requirement? – Eugene Sh. Mar 04 '16 at 21:24
  • And can you describe the requirement a bit more? I seem to get what you are asking, but don't quite get what you mean to say with the ")" and ",arg)" part of your question. – tofro Mar 04 '16 at 21:31
  • @EugeneSh. It's difficult without rat-holing, but I've added a short example. – geometrian Mar 04 '16 at 21:48
  • 3
    Why do they need the same name? I don't understand what the intent is. Maybe if you described the problem you're trying to solve with this? – Schwern Mar 04 '16 at 21:51

2 Answers2

1

You seem to be looking for a preprocessor that has different name spaces for function- (with arguments) and object-like (plain #defines) macros - I don't know of any that would have that, and it would be fundamentally confusing if there was one, I assume.

I understand you want something like (note this is actually not trying to provide a solution)

// NOTE: Example might work in some very trivial use cases
#ifdef DEBUG
#define new(x) new(x);lognew(__FILE__,__LINE__)  
#define pnew(p,x) new (p) (x); lognew(__FILE__,__LINE__)
#else
#define new(x) new(x)
#define pnew(p,x) new(p) (x)
#endif

And pnew and new should use the same keyword. No, I don't think that is possible (and I'm glad it isn't ;) ).

tofro
  • 5,640
  • 14
  • 31
  • Currently accepting this answer, as it makes a claim that this is impossible. This has been my conclusion as well. If anyone figures out a way to do it, I'll accept that instead. – geometrian Mar 06 '16 at 19:51
-1

Functions with variable signatures are only allowed in C++ , called "function overloading".

Arif Burhan
  • 507
  • 4
  • 12