macros - Comparing Types Of Names In C -
i'm trying write macro in c (alas, not c++) in way trap errors, in particular if pass name of wrong type.
for example, with
typedef int aplnelm; typedef int aplrank; #define isscalar(a) ((a) == 0) aplnelm aplnelm = 0; aplrank aplrank = 0; calling isscalar (aplrank) correct because scalar rank concept, isscalar (aplnelm) wrong because scalar not # elements concept.
can clever person find way write isscalar macro such checks type of name passed ensure of type aplrank? feel free rewrite original example in equivalent way if provides solution.
if these 2 types ever passed isscalar macro, this:
#include <stdio.h> struct aplnelm { int nelm; char a[1]; }; struct aplrank { int rank; char a[2]; }; #define isscalar(b) (sizeof b.a == 2) int main(void) { // code goes here struct aplnelm temp1; struct aplrank temp2; printf("%d\n", isscalar(temp1)); printf("%d\n", isscalar(temp2)); return 0; } the output of code is
0 1
Comments
Post a Comment