0

I'm trying to write a macro that expands into a safe way to call a block.

#define callBlockSafely(blockName, args...) \
if (blockName)                              \
{                                           \
    blockName(##args);                      \
}

However, when I try to use this, I get this error:

"Pasting formed '(1', an invalid preprocessing token"

So, what are alternative ways of achieving this?

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • 1
    Define a *method* instead and use that one! – luk2302 May 26 '16 at 17:49
  • 1
    Why do you think you need to use `try/catch`? You should fix that problem. – rmaddy May 26 '16 at 17:49
  • @rmaddy This is one of two macros; the other doesn't have the try-catch. I suppose that's excessive for this example; I'll pare it down. – Ky - May 26 '16 at 17:56
  • It also eliminates the need for a macro. – rmaddy May 26 '16 at 17:57
  • @rmaddy Could you elaborate? – Ky - May 26 '16 at 17:57
  • 1
    What's the point of a macro just to eliminate the `if`? Instead of `callBlockSafely(callback, 1, nil)` you just need `if (callback) callback(1);`. Do you really think the macro saves anything now? – rmaddy May 26 '16 at 17:59
  • @rmaddy Our coding standards require every `if` to be on its own line, followed by a `{` on its own line, followed by the contents, followed by a `}` on its own line. I don't like this since it makes line counts higher than they need to be, and this is one way of reducing that. And again, a sister macro will have the try-catch with naught but a few characters difference in the name – Ky - May 26 '16 at 18:17
  • Even with your coding standard, there is no point for a macro just to avoid having to type the `if { ... }`. – rmaddy May 26 '16 at 18:24

0 Answers0