fork download
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct my_type_struct my_type;
  6.  
  7. void constructor1 ( my_type * object, uint16_t arg1 );
  8. void constructor2 ( my_type * object, uint16_t arg1, uint16_t arg2 );
  9. void constructor3 ( my_type * object, const void * const array_arg1,
  10. uint16_t arg2, uint16_t arg3 );
  11.  
  12. #define GET_CONTRUCTOR_OVERLOAD(_1, _2, _3, _4, NAME, ...) NAME
  13. #define INIT_OBJECT(...) GET_CONTRUCTOR_OVERLOAD(__VA_ARGS__, \
  14.   constructor3, constructor2, constructor1)(__VA_ARGS__)
  15. #define CONSTRUCTOR(name, ...) my_type name; INIT_OBJECT(&name, __VA_ARGS__)
  16.  
  17. struct my_type_struct
  18. {
  19. int field;
  20. };
  21.  
  22. int main ( int argc, char* argv[] )
  23. {
  24. uint16_t n = 13;
  25. uint16_t m = 14;
  26. int c[30];
  27. CONSTRUCTOR ( obj, c, n, m );//invokes constructor1
  28. //system ( "pause" );
  29. }
  30.  
  31. void constructor1 ( my_type * object, uint16_t arg1 )
  32. {
  33. printf ( "%s\n", __func__ );
  34. object->field = 1;
  35. }
  36. void constructor2 ( my_type * object, uint16_t arg1, uint16_t arg2 )
  37. {
  38. printf ( "%s\n", __func__ );
  39. object->field = 2;
  40. }
  41. void constructor3 ( my_type * object, const void * const array_arg1,
  42. uint16_t arg2, uint16_t arg3 )
  43. {
  44. printf ( "%s\n", __func__ );
  45. object->field = 3;
  46. }
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
constructor3