c - Pointer to a function that accepts both const and non-const arguments -
i want write wrapper read , write unix functions, read has const void pointer parameter, , write simple void pointer parameter.
so, prototype this, fail 1 of functions:
typedef ssize_t (*genericstreamhandler)(int, const void*, size);
do not prototype function signature if code needs allow incompatible functions.
the following compiles without warnings/errors.
#include <stdio.h> ssize_t file_read(int h, const void* b, int sz) { if (b) return 0; return h + sz; } ssize_t file_write(int h, void* b, int sz) { if (b) return 0; return h + sz; } //typedef ssize_t (*genericstreamhandler)(int, void*, int); // v--- no function prototype typedef ssize_t (*genericstreamhandler)(); int main(void) { genericstreamhandler gfh1 = file_read; genericstreamhandler gfh2 = file_write; char buf[10]; return (*gfh1)(0, buf, 10) + (*gfh2)(0, buf, 10); }
otoh, better answer may lie in taking approach not need common type variant function signatures.
Comments
Post a Comment