How to prevent macros from hiding 'const' when casting (C) -
i have macro takes 2 args, 1 value modified , offset have cast.
#define my_macro(dst, src) \ do_something((char *)dst + offset, (char * )src + offset) in illustration above, dst modified, if value const, cast (char *) hide this.
however, don't want macro silently hide const members.
static void my_function(const float *a, const float *b) { my_macro(a, b); /* <-- should warn because 'a' const */ } i wondering if there way ensure variables cast within macro not hiding const.
moved illustration solution own reply - http://stackoverflow.com/a/25072965/432509
what's 1 thing can't const types? assign them. therefore, how compiler's attending shouldn't using const pointers in position? seek assign through them!
add line definition:
#define my_macro(dst, src) \ ((void)(0 ? ((void)(*(dst) = *(dst)), *(src) = *(src)) : 0), \ do_something((char *)dst + offset, (char * )src + offset)) because of 0 ? ..., inserted line never (and void casts shouldn't trigger warnings either), it's c-level look , means compiler must check before optimization begins; fact never runs, has no effects, , removed before code generation doesn't mean it's allowed skip type check. pointers non-const pass.
the value beingness assigned sourced same pointer work type; since line never runs, won't have multiple-evaluation problems multiple appearances of name. using ?: instead of if means can set in comma look in case do_something needs homecoming value.
c macros casting constants
No comments:
Post a Comment