Question

I never saw this ## pattern before and in any books I read. So what does this ## mean? I cannot understand these pieces of code involving ##:

#define DECLARE_STRUCT_MRC_CLASS(sfx, obj_type)         \
  struct mrc_class ## sfx {                             \
    const char *name;                                   \
    list_t subclasses;                                  \
    list_t instances;                                   \
    size_t size;                                        \
    bool initialized;                                   \
    void (*create)(obj_type *);                         \
    void (*destroy)(obj_type *);                        \
  }

and

DECLARE_STRUCT_MRC_CLASS(, struct mrc_obj);

#define MRC_CLASS_DECLARE(pfx, obj_type)                                \
  obj_type;                                                             \
  DECLARE_STRUCT_MRC_CLASS(_ ## pfx, obj_type);                         \
                                                                        \
  extern struct mrc_class_ ##pfx mrc_class_ ## pfx;                     \
  static inline obj_type *                                              \
  pfx ## _create(MPI_Comm comm)                                         \
  {                                                                     \
    return (obj_type *)                                                 \
      __mrc_obj_create(comm, (struct mrc_class *) &mrc_class_ ## pfx);  \
  }                                                                     \

Any clarification is appreciated. Thanks in advance!

Was it helpful?

Solution

Token pasting. It literally glues together the two tokens on either side of it, to form what looks to the compiler like a new, single token:

pri ## ntf("Hello, world!\n");

Once you add the usual boilerplate to the above, it should compile and do the expected thing.

OTHER TIPS

It is used to concatenate tokens. Hers is a detailed explaination on it: http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top