1

I read through some sample code, and caught up with this ## syntax that I have never seen before. The code sample as following:

#define DEF_MAKE_BT_1_COMP(BT)\
inline i_##BT make_i_##BT(i_##BT::vtype x) { return make_##BT(x); }

#define DEF_MAKE_BT2(T)         \
DEF_MAKE_BT_1_COMP(T##1);           \
DEF_MAKE_BT_2_COMP(T##2);

DEF_MAKE_BT2(double);

When I got error at the last line which is "make_double1" is undefined. Has anyone seen this syntax before?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
OhMyGosh
  • 1,579
  • 4
  • 18
  • 31
  • 3
    Token concatenation. Asked and answered several times on SO. The difficulty, as ever, will be finding the right question to make this a duplicate of. – Jonathan Leffler May 11 '15 at 05:12
  • concatenate lexically. https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html – v.oddou May 11 '15 at 05:13
  • See [C preprocessor and token concatenation](http://stackoverflow.com/questions/1489932/). – Jonathan Leffler May 11 '15 at 05:14
  • searching for this is not very hard [What does 'a ## b' mean in C?](http://stackoverflow.com/q/26576201/995714) [What do two adjacent pound signs mean in a C macro?](http://stackoverflow.com/q/9059772/995714) [What does ## (double hash) do in a preprocessor directive?](http://stackoverflow.com/q/22975073/995714) – phuclv May 11 '15 at 05:15
  • 1
    @LưuVĩnhPhúc - if I recall, Google does not allow special characters, like pound sign, in search. Or it does not honor them. For example, [what does ## mean in C](http://www.google.com/search?q=what+does+%23%23+mean+in+C). The first ***five*** pages have ***0 relevant results***. You have to use a different search engine for the best results. Here, "best" is related to the original search terms and not the answers. – jww May 11 '15 at 05:18
  • 1
    @jww you can search some special symbols on Google although this is a bit harder so I need to use "double hash symbol", it works. You can also use a [search engine for special characters](http://stackoverflow.com/q/4685615/995714) – phuclv May 11 '15 at 05:20
  • Hi guys, thanks for all your replies! Actually, I tried to search for ## a lot of times, but I cannot get any related links for ##. I guess the reason is as @jww explaination. – OhMyGosh May 11 '15 at 06:43

1 Answers1

3

## is a pre-processor operator that concatenates two tokens.

From http://en.cppreference.com/w/cpp/preprocessor/replace

A ## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers (which are not macro-expanded first) and then concatenates the result. This operation is called "concatenation" or "token pasting". Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier, digits that form a number, or operators + and = that form a +=. A comment cannot be created by pasting / and * because comments are removed from text before macro substitution is considered. If the result of concatenation is not a valid token, the behavior is undefined.

R Sahu
  • 204,454
  • 14
  • 159
  • 270