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

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -