Comment

Comment by Someone

You can do that since C11 using _Generic. https://en.cppreference.com/w/c/language/generic:

  #include <math.h>
  #include <stdio.h>
 
  // Possible implementation of the tgmath.h macro cbrt
  #define cbrt(X) _Generic((X), \
              long double: cbrtl, \
                  default: cbrt,  \
                    float: cbrtf  \
              )(X)
 
  int main(void)
  {
    double x = 8.0;
    const float y = 3.375;
    printf("cbrt(8.0) = %f\n", cbrt(x)); // selects the default cbrt
    printf("cbrtf(3.375) = %f\n", cbrt(y)); // converts const float to float,
                                            // then selects cbrtf
  }
parent Someone 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton