250
250

Reputation: 659

C++ macro '##' doesn't work after '->' operator

I have a shared_ptr object x, which has get and set methods as follows:

x->a_value();
x->set_a_value();
x->b_value();
x->set_b_value();

When i try to define a macro:

#define MAC(type) \
  x->set_##type##_value(val);

MAC(a)

It works fine, but when I do:

#define MAC(type) \
  x->##type##_value();

MAC(a)

it gives the following compile error: pasting formed '->a', an invalid preprocessing token

Upvotes: 24

Views: 3774

Answers (3)

cute_ptr
cute_ptr

Reputation: 1203

The token-pasting operator (##) is used to concatenate two tokens into a single valid token.

When you write

x->##type##_value();
  • The first processed token is x.

  • The next token is formed by concatenating the token -> with type, since type is a, the result of the concatenation is ->a, which ought to be a valid token, but is not.

Hence, you get the error: pasting formed '->a', an invalid preprocessing token.

To fix this, just write

x->type##_value();

This way

  • The first token parsed is x.

  • The next token parsed is ->.

  • The next token is formed by concatenating the token type (which becomes a) with the token _value. This gives a_value, which is a valid token.

  • The next token is (.

  • The next token is ).

  • The last token is ;.

Upvotes: 1

Bo Persson
Bo Persson

Reputation: 92381

The preprocessor works on "tokens" - likes names and operators.

The ## operator creates a new token by pasting smaller parts together. In the first example set_##type##_value becomes set_a_value, which is a valid token.

In the second example ->##type##_value would become ->a_value, which is not a valid preprocessor token. It ought to be two tokens.

If you just make the line x->type##_value(); it should work. You get the separate tokens x, ->, a_value, (, ), and ;.

Upvotes: 52

Quentin
Quentin

Reputation: 63154

What it says on the tin: ->a is not a single, valid preprocessor token: it's two tokens. You do not need to paste here.

#define MAC(type) \
  x->type##_value();

Upvotes: 17

Related Questions