-2

to make my question precise i would like to create a macro that accepts as variable ++ and pastes it with another variable. In code:

#define MYMACRO(A, OP) /* .... */

and then if i write in the source file

MYMACRO(this->var, ++)

the preprocessor should paste in the code

this->var++;

When i am trying to define the macro as A##OP , it provides the following error:

pasting "++" and "var" does not give a valid preprocessing token

Is it possible to do what i am trying?

thanks in advance for the answer

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    Your macro would not make sense. – Iharob Al Asimi Sep 07 '16 at 15:44
  • 2
    Are you using C or C++? In C++ I would suggest making a template function. – NathanOliver Sep 07 '16 at 15:45
  • 1
    `##` is used for pasting two tokens into a single token, e.g. `MYMACRO(foo, bar)` results in the variable `foobar`. But `this->var++` is multiple tokens, so you can't paste them together like that. It doesn't work at the text level, so it doesn't just put things next to each other and reparse it. – Barmar Sep 07 '16 at 15:47
  • 1
    There is no place for macros such as this in c++. You're 15 years behind. – Richard Hodges Sep 07 '16 at 16:32

2 Answers2

6

You don't need the ## token pasting operator, because you're not trying to combine the parameters into a single token. Just use:

#define MYMACRO(a, op) (a)op;
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 4
    I'm pretty sure you *would have preferred to answer that as* `#define MYMACRO(a, op) ((a)op)` instead (but what the OP wants is kind of silly, including the semicolon in the macro). – Nominal Animal Sep 07 '16 at 15:52
  • Technically you're not doing what the OP asked for. The OP's example does *not* introduce parentheses. – EOF Sep 07 '16 at 15:58
  • @EOF True, but parentheses are needed in case `a` has subexpressions that have lower precedence than the operator. – Barmar Sep 07 '16 at 16:04
  • @Barmar Unless the OP *wants* `MYMACRO(*ptr, ++)` to modify the pointer rather than the pointed-to object. In the absence of anything the OP has said about this, you should *probably* not introduce bugs for them. – EOF Sep 07 '16 at 16:35
  • If the parentheses are unwanted, a space would work too: `#define MYMACRO(a, op) a op` – Dmitri Sep 07 '16 at 16:38
  • @EOF Without specific information to the contrary, I assume that people want function-like macros to be parsed similar to functions, where the comma has the lowest precedence. Failing to add parentheses is usually the *cause* of unexpected behavior. – Barmar Sep 07 '16 at 21:04
  • @EOF http://stackoverflow.com/questions/36862207/bad-parentheses-in-macro-definition?s=1|1.8332 – Barmar Sep 07 '16 at 21:07
  • thanks @Barmar. Thats what i was looking for. Although i had already somehow found a way around it to do what i needed – Georgios Chatzigiannis Sep 09 '16 at 06:59
  • I wonder why you accepted the other answer instead of mine. The code is the same, but I added more explanation of the problem with your code. – Barmar Sep 09 '16 at 16:12
0

You just need to combine the tokens like this:

#define MYMACRO(a, incrdecr) (a)incrdecr
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88