3

The #define creates a macro, which is the association of an identifier or parameterized identifier with a token string. After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.

https://learn.microsoft.com/en-ca/cpp/preprocessor/hash-define-directive-c-cpp?view=vs-2017

Surprisingly, the question has not been directly asked, rather asking about tokenization, tokenizer, tokening etc. Even searching on DuckDuckGo, the closest question was on quora asking,

What is a string token in c++?

And it is not obvious to me whether string token and token string would be synonymous. So just to be clear:

What is a token string in c++?

Anon
  • 2,267
  • 3
  • 34
  • 51
  • 1
    The compiler deoes no such thing. That's the job of the preprocessor. If you want to find out what the compiler gets to see after the preprocessro run `gcc -E foo.c` – Swordfish Dec 04 '18 at 19:04
  • The answer to the Quora link was written by someone who appears to have wanted to answer a different question. – user4581301 Dec 04 '18 at 19:06
  • Addendum to @Swordfish 's comment: [The Visual Studio version](https://learn.microsoft.com/en-us/cpp/build/reference/p-preprocess-to-a-file?view=vs-2017) – user4581301 Dec 04 '18 at 19:10
  • 2
    And keep an eye out for the Tolkien strings. They're the short ones with hairy feet. – user4581301 Dec 04 '18 at 19:11
  • 3
    One (st)ring to rule them all, one (st)ring to find them, One (st)ring to bring them all and in the darkness bind them. – Swordfish Dec 04 '18 at 19:13
  • 2
    @Swordfish that's... precious. – user4581301 Dec 04 '18 at 19:24

1 Answers1

4

In this case the token string is the macro body. In

#defined MAKE_MY_FUNC(x) void x(int bar)

The void x(int foo) part would be considered the token string and when you use MAKE_MY_FUNC like

MAKE_MY_FUNC(foo){ std::cout << bar; }

then the token string would be subsituted in and the code would be transformed into

void foo(int foo){ std::cout << bar; }

Your article gives you what they call the token-string in the second paragraph

The token-string argument consists of a series of tokens, such as keywords, constants, or complete statements. One or more white-space characters must separate token-string from identifier. This white space is not considered part of the substituted text, nor is any white space that follows the last token of the text.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402