macro to define a struct in c or c++ -
macro to define a struct in c or c++ -
is possible define struct variable size of array. this:
#define char_array(max_length) struct char_array_#max_length{char data[max_length]; int length;};
so above macro, define different info types follows.
struct data{ char_array(4) a; char_array(8) b; };
due special requirements of existing code, plain old type defined "struct" required, no utilize of std::array, vector, etc. in particular, pointer not accepted our legacy code has requirement plain old type, assign or re-create can handled.
yes it’s possible that. don’t need name struct @ all, makes macro simpler:
#define char_array(max_length) struct {char data[max_length]; int length;}
(usage in example.)
however, i’m still not exclusively clear purpose of macro, since re-implements, badly, existing functionality. @ to the lowest degree should utilize template instead of macro generate type you:
template <std::size_t n> struct char_array { char data[n]; std::size_t length; };
and create element type generic:
template <typename t, std::size_t n> struct array { t data[n]; std::size_t length; };
… , it’s becoming blindingly obvious bad re-implementation of std::array
, , there no benefit in not using latter.
c++ c macros preprocessor
Comments
Post a Comment