... and the preprocessor?

Depending on if #define _MODE_1 is set I want to call different versions of a function. At the moment I use

#if defined _MODE_1
myfunction(x,a,n,...);
#else
myfunction(a,n,...);
#endif

The ellipsis carries different arguments at different posions - but within the #if the arguments allways agree (only the x differs). Sometimes there are no arguments in the ellipsis.

I would like to define a macro MyFUNCTION that does the #if such that is does not appear in the the source. something like:

#define MyFUNCTION(X,A,N,ELL) #if defined _MODE_1\
myfunction(X,A,N,ELL)\
#else\
myfunction(A,N,ELL)\
#endif

or

#if defined _MODE_1
#define MyFUNCTION(X,A,N,ELL) myfunction(x,a,n,ell)
#else
#define MyFUNCTION(X,A,N,ELL) myfunction(a,n,ell)
#endif

such that I can just write

MyFUNCTION(x,a,n,e1,e2,e3);

in the source.

Is something like this possible? How to treat the '...' and its preceding ',' in such a case?
I'm sorry for such question, but why do you want such a "super-macro"?

You will be necessarily passing spare "x" argument at each place where you call the function? why not make "if...else..." inside the function and make it call two other functions. Obvious profit is that you can use both versions at runtime...
closed account (zb0S216C)
Why don't you use variadic templates[1]? They're intended to replace C's "va_list".

[1] http://www.cplusplus.com/articles/EhvU7k9E/

Wazzak
Last edited on
Why can't you just supply a function overload that does what you want?

1
2
3
4
5
6
7
inline int MyFUNCTION( type1 x, type2 a, type3 n, type4 e1, type4 e2, type4 e3 )
{
    if ( _MODE_1 )
        myfunction(x, a, n, e1, e2, e3) ;
    else
        myfunction(a, n, e1, e2, e3);
}


[Edit: This can be disregarded. I didn't read the OP closely enough.]
Last edited on
@Framework: Thanks, that solves the problem.
Topic archived. No new replies allowed.