Hacker News new | ask | show | jobs
by nstbayless 1048 days ago
Will this work?

  define string_length(x) _Generic(x,        \
    const char *            : strlen((const char*)(const void*)x),           \
    struct MyStringBuffer * : ((const MyStringBuffer*)(const void*)x)->length)
1 comments

Heh, this was my first thought as well, and it does indeed compile and work (with GCC 12.2, anyway).
Yeah, this is what I saw in some example code. Also you don't need the (void*), at least not with clang or GCC trunk on compiler explorer: https://godbolt.org/z/a8rP91dKr