4 ms·
C type declarations are quite sane, until you need function pointers. Then things get really crazy. Consider this type declaration in Go: func(string) []f
by daghamm 2y ago
C type declarations are quite sane, until you need function pointers. Then things get really crazy.
Consider this type declaration in Go:
func(string) []func(int)[]*float64
This took me 5 seconds to write. The C/C++ counterpart would probably take an hour to get right :(
- lieks 2y agoHmm... Let's see... Oh, functions can't return arrays in C. That would be a pointer. Well, that makes it easier. double **(**(*)(char *))(int) It did take a couple minutes, but I just woke up. Also, idiomatically there would be a typedef: typedef double **A(int); typedef A **B(char *); B * That took a few seconds to write.
- Arch-TK 2y agoFunction pointers are easy if you typedef the function type. What a pointer to function (int) returning int? The difficult way: int (*p)(int); The easy way: typedef int Func(int); Func *p; The typedef reads just like a normal function declaration except for the name is the type name rather than the function symbol name. Just like a function declaration, you can add more documentation by giving the parameters names. e.g.: // used for some hypothetical allocator API typedef void *Alloc(size_t size); Similar trick can be used for pointers to arrays (just typedef the array type). Although in practice pointers to arrays just make for awful code.