0

Any work around to make the below code work? it currently give the following error:

error: too many arguments provided to function-like macro invocation
         X({1,2,3});
                             ^

code:

#include <stdio.h>
#define X(a) a,

int main()
{
    mystruct = X({1,2,3}));
}

I tried something with templates but so far I know (I'm a bit new to C++) templates aren't "powerful" enough to implement something like the X macro. As you may already noticied, what I'm looking for is implement something like the X macro. Others ideas to do this are very welcome too as long as everything is know at compile-time.

The Mask
  • 17,007
  • 37
  • 111
  • 185
  • 4
    What is the problem you are trying to solve here? You might find that there is another technique that solves your real problem better (and there usually is if you are using macros). – David Stone Apr 13 '14 at 15:23
  • 2
    Assume it is allowed, what do you expect `printf` to print? – Nawaz Apr 13 '14 at 15:25
  • _'templates aren't "powerful" enough ...'_?? Could you elaborate on this please IMHO it's just the opposite situation. – πάντα ῥεῖ Apr 13 '14 at 15:25
  • @DavidStone: the X macro as I mentioned in the question – The Mask Apr 13 '14 at 15:31
  • @Nawaz: It was I typo due my code editing on SO to create the question – The Mask Apr 13 '14 at 15:32
  • @πάνταῥεῖ: For the reason I mentioned in the question: you can't implement the X macro using this. But I can found a word better than "powerful" to say that – The Mask Apr 13 '14 at 15:33
  • @TheMask Possibly related: http://en.wikipedia.org/wiki/Variadic_macro http://stackoverflow.com/questions/4786649/are-variadic-macros-nonstandard – πάντα ῥεῖ Apr 13 '14 at 15:33
  • Yes, it can, but it will be read as `X( {1, 2, 3} )`, with the braces being part of the individual arguments. I'm not sure what you're going for (%zu doesn't work with an initializer list), but there are type-safe `printf` implementations. – chris Apr 13 '14 at 15:33
  • @TheMask The only _reason_ you state is _'I tried something with templates'_ :-/ ... – πάντα ῥεῖ Apr 13 '14 at 15:37
  • 1
    If you simply make `X` `#define X(...) __VA_ARGS__,`, then the macro expansion will succeed. But you will then get `printf("%zu\n", {1,2,3},);` or `mystruct = {1,2,3},);`, both of which have syntax errors. I cannot guess from the question what the result is supposed to be. –  Apr 13 '14 at 15:39
  • @hvd: It's the solution. Expanded exactly as I needed! post as answer. It should have literal output of an argument to I implement the X macro with additional values set by macro of course – The Mask Apr 13 '14 at 15:51
  • 1
    πάνταῥεῖ has posted it as an answer already, so I'll refrain from posting a duplicate answer. –  Apr 13 '14 at 15:52

1 Answers1

1

This does the trick:

#define X(...) __VA_ARGS__

struct mystruct {
    int a,b,c;
};

int main() {
    mystruct s = X({1,2,3});
}

Or as a variation:

#define X(...) {__VA_ARGS__}

struct mystruct {
   int a,b,c;
};

int main() {
   mystruct s = X(1,2,3);
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I posted that as a comment instead of an answer, because I think it is not at all what the OP is looking for: I see no way for initialiser lists and `printf` to be meaningfully combined. (Edit: as it turns out, it's exactly what the OP is looking for, but the question was misleading.) –  Apr 13 '14 at 15:50
  • Only thing is I don't see why the macro exists at all then. That's the OP's question not making sense, though. – chris Apr 13 '14 at 15:52
  • 1
    I said in comments printf() was actually a typo (I edited code here on OS and forget to remove printf()) – The Mask Apr 13 '14 at 15:52