Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: