![]() Now you have to duplicate everything which makes it less useful for categorization globally.Ĭheating with #define _avg_map _num_map does not work, neither does nesting _num_map deeper. Having macros like _num_map that are used everywhere is nice for when you just need "a function that take numeric types" or you could do something like _integer_map and _floating_map for the other types, which lets you easily categorize the types.Because the list of input types are identical, we end up with more code duplication.avg$ should inherit the list of acceptable input types from sum$ as it will only support the types that sum$ supports.I don't like this solution for three reasons: Resolving the problem involves duplicating _num_map: // Define avg functions So for some reason gcc doesn't want to keep expanding _num_map when it is already expanding _num_map. _num_map(_def_avg,) fails to expand properly, looking at the output of gcc -E sum-generic.c I can see that _num_map is treated as a function call when sum$ is expanded inside _def_avg. #define _gen_avg(_T) _gencase(_T, _avg_func) The problem comes when I attempt to do something like this: // Define avg functionsĭouble _avg_func(_T)(_T *xs, size_t len) \ Printf("Zero DSum: %lf\n", suma$(dubs0)) This works pretty well and can be used like this: int main(void) The $ symbol at the end of sum$ and suma$ is just a personal style-guide that means "generic macro, be aware of possible weird compiler errors/warnings if you use it wrong." It is essentially just a list, with built-in macro-mapping, think of the _ character as, while the $ character is the mapped macro. _num_map is used in order to avoid having to duplicate the types in both the function definition macro and the _Generic expression. #define _gen_sum(_T) _gencase(_T, _sum_func) #define _alen(_xs) sizeof(_xs)/sizeof(_xs) ![]() Here is the sum macro with all the helper macros: // Helper macros ![]() I wrote a generic sum macro using the C11 _Generic keyword, but I'm having problems expanding the macro inside other generic function definitions. It might help to identify which compiler youre using does it recognize C99/C++ comments marked by // to end of line Also, Im fairly sure youve not minimized the code the PUT macro doesnt use any of the others, so you should be able to eliminate all but the last line of the code you show.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |