3

Is it possible to define something like this:

#define FOO(x, y) BAR()
#define FOO(x, sth, y) BAR(sth)

so that this:

FOO("daf", sfdas);
FOO("fdsfs", something, 5);

is translated to this:

BAR();
BAR(something);

?

Edit: Actually, BAR's are methods of my class. Sorry for not saying that before (didn't think it was relevant).

Answering DyP's question:

class Testy
{
    public:
    void TestFunction(std::string one, std::string two, std::string three)
    {
        std::cout << one << two << three;
    }
    void AnotherOne(std::string one)
    {
        std::cout << one;
    }
    void AnotherOne(void)
    {
        std::cout << "";
    }
};

#define PP_NARG(...) PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N(_1, _2, _3, N, ...) N
#define PP_RSEQ_N() 3, 2, 1, 0

// macro for exactly 2 arguments
#define FOO_2(_1, _2) AnotherOne()
// macro for exactly 3 arguments
#define FOO_3(_1, _2, _3) AnotherOne(_2)

// macro selection by number of arguments
#define FOO_(N) FOO_##N
#define FOO_EVAL(N) FOO_(N)
#define TestFunction(...) FOO_EVAL(PP_NARG(__VA_ARGS__))(__VA_ARGS__)

And call:

Testy testy;
testy.TestFunction("one", "two", "three");   // line 9

Compiler output:

Warning 1 warning C4003: not enough actual parameters for macro 'PP_ARG_N' main.cpp 9

Warning 2 warning C4003: not enough actual parameters for macro 'FOO_' main.cpp 9

Error 3 error C2039: 'FOO_' : is not a member of 'Testy' main.cpp 9

2
  • It's possible to let it expand to BAR(sfdas); and BAR(something, 5); respectively. Is this acceptable? Macros can't "discard" the last parameter, but they can discard the first.
    – leemes
    May 4, 2013 at 13:29
  • See also stackoverflow.com/questions/3850421/…
    – leemes
    May 4, 2013 at 13:31

1 Answer 1

10

Edit ------------------------------------------------look here----------------------------------------------------------------->

(Overloading macro on number of arguments)

// functions, or macros, ....
void bar(){}
void bar(int){}

#define EXPAND(X) X    // for MSVC10 compatibility

// compute number of (variadic) macro arguments
// from http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a3fb/346fc464319b1ee5?pli=1
#define PP_NARG(...) EXPAND( PP_NARG_(__VA_ARGS__, PP_RSEQ_N()) )
#define PP_NARG_(...) EXPAND( PP_ARG_N(__VA_ARGS__) )
#define PP_ARG_N(_1, _2, _3, N, ...) N
#define PP_RSEQ_N() 3, 2, 1, 0


// macro for exactly 2 arguments
#define FOO_2(_1, _2) bar()
// macro for exactly 3 arguments
#define FOO_3(_1, _2, _3) bar(_2)

// macro selection by number of arguments
#define FOO_(N) FOO_##N
#define FOO_EVAL(N) FOO_(N)
#define FOO(...) EXPAND( FOO_EVAL(EXPAND( PP_NARG(__VA_ARGS__) ))(__VA_ARGS__) )

int main()
{
    int something = 42;
    FOO("daf", sfdas);
    FOO("fdsfs", something, 5);
}

Preprocessor output:

void bar(){}
void bar(int){}

int main()
{
    int something = 42;
    bar();
    bar(something);
}

Edit2: Seems like VS2010 has some issues with __VA_ARGS__ and macro replacement.

UPDATE: It's ... a bug?? (also see this SO question):

#define MACRO2(PARAM0, PARAM1, ...) arg 0: >PARAM0<    arg 1: >PARAM1<    \
  additional args: >__VA_ARGS__<
#define MACRO1(...) MACRO2(__VA_ARGS__, OTHERARG_0, OTHERARG_1)

MACRO1(ARG0, ARG1);

Preprocessor output:

arg 0: >ARG0, ARG1<    arg 1: >OTHERARG_0<    additional args: >OTHERARG_1<;

For a workaround, see the linked SO question. I've updated the original answer (code) above and tested it with MSVC10 -> works now.

5
  • I've edited my question - BAR is a method of a class and your solution doesn't work for that (compiler says something about 'FOO_PP_NARG' not being a member of 'MyClass').
    – NPS
    May 4, 2013 at 16:43
  • Compiler VS2010, the rest I provided in my question.
    – NPS
    May 7, 2013 at 15:38
  • 1
    @NPS See my update. I'll try to find out what's the reason, though I guess it's a bug / non-standard behaviour in the VS2010 preprocessor.
    – dyp
    May 7, 2013 at 17:05
  • This one works, thanks, but it still shows a warning: warning C4003: not enough actual parameters for macro 'FOO_2' (or FOO_3).
    – NPS
    May 7, 2013 at 17:32
  • @NPS Please take a look at my (new) update. The old macro was buggy AFAIK.
    – dyp
    May 7, 2013 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.