C language: cast functions, again

Posted on Sun Feb 19, 2017

Almost 8PM, the right time for a followup to the post of this morning, C language: cast functions.

Why am I using an inline function and not just defining a casting macro as follows?

#define cast_to_beta(SRC)               \
  _Generic((SRC), alpha_t *: (beta_t *))(SRC)

It is not only early–morning thinking… Casting a pointer is a special case of type conversion, it is possible to generalise pointer–casting to full object–representation conversion; in this case the conversion may require a full function not integrable at the call site.

This obviously would introduce the problem of resources allocation management: what happens to the old object? Is it destroyed? Is it still there? Pointer casting and representation conversion may have different semantics, which would be bad to use in the same macro:

#define cast_to_beta(SRC)                       \
  _Generic((SRC),                               \
           alpha_t *: (beta_t *),               \
           gamma_t *: gamma_to_beta)(SRC)

Will I actually define full–type conversion like this? I do not know. This is just the beginning of an experiment…