1

I have this macro for start packet's

#define BEGIN_PACKET_DEF(name, type) typedef struct name {\
int8_t header;\
static size_t size() { return sizeof(name); }\
name() { memset(this, 0, sizeof(name)); header = type; }

And this for ending packet's

#define END_PACKET_DEF(name) } name;

And i want to change it as a c++ function. As name says is an define to start packet definition The BEGIN_PACKET_DEF will add auto the int8_t header; in each packet

Example usage :

    BEGIN_PACKET_DEF(Name_struct,HEADER)
 // some packet's data
   END_PACKET_DEF(Name_packet)

I need some help to rewrite those macro as C++ functions and to do same effect like macros, I just don't like to use macros.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
fraky
  • 3
  • 5
  • One hint might be to ask your compiler to show the source text after preprocessing - as this contains the expanded macros ... – Dilettant Sep 16 '16 at 16:10

1 Answers1

2

I just don't like to use macros.

This kind of BEGIN_ END_ type macro pairs is one of their rare valid uses, and you generally can't replace them with functions to achieve the same effect.


The closest you can get in c++ without a macro, is to define a variadic template struct, that forwards the type parameters to a std::tuple. You can see my old question1 here how that can be done.


1)Don't care about the downvotes, the question was valid, and the answer was good. Actually that had been used to go in production.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190