GCC内联函数:__builtin_types_compatible_p

#if 0
— Built-in Function: int __builtin_types_compatible_p (type1, type2)
You can use the built-in function __builtin_types_compatible_p to determine whether two types are the same.
This built-in function returns 1 if the unqualified versions of the types type1 and type2 (which are types, not expressions) are compatible, 0 otherwise. The result of this built-in function can be used in integer constant expressions.
This built-in function ignores top level qualifiers (e.g., const, volatile). For example, int is equivalent to const int.
The type int[] and int[5] are compatible. On the other hand, int and char * are not compatible, even if the size of their types, on the particular architecture are the same. Also, the amount of pointer indirection is taken into account when determining similarity. Consequently, short * is not similar to short **. Furthermore, two types that are typedefed are considered compatible if their underlying types are compatible.
An enum type is not considered to be compatible with another enum type even if both are compatible with the same integer type; this is what the C standard specifies. For example, enum {foo, bar} is not similar to enum {hot, dog}.
You would typically use this function in code whose execution varies depending on the arguments' types. For example:
          #define foo(x)                                                  \
            ({                                                           \
              typeof (x) tmp;                                             \
              if (__builtin_types_compatible_p (typeof (x), long double)) \
                tmp = foo_long_double (tmp);                              \
              else if (__builtin_types_compatible_p (typeof (x), double)) \
                tmp = foo_double (tmp);                                   \
              else if (__builtin_types_compatible_p (typeof (x), float))  \
                tmp = foo_float (tmp);                                    \
              else                                                        \
                abort ();                                                 \
              tmp;                                                        \
            })

Note: This construct is only available for C.
#endif

代码:

#include <stdio.h>
#include <fcntl.h>
#define  TYPE(x)     typeof(x)


int main(void)
{
//	int a = 100 ; 
//	
//	//typeof(a)   b = 200 ; 	
//	TYPE(a)  b = 200 ; 
//
//	printf("a:%d  b:%d \n" , a , b );


	int a = 100 ; 
	int b = 200 ; 
	double c = 100.1 ;
	int fd ; 
	fd = open("txt",O_CREAT | O_RDWR); 
	if(-1 == fd)
	{
		fprintf(stderr , "haha!\n"); 
		return -1 ; 
	}

	//类型相同的情况下返回1     类型不相同时返回0
	printf("%d  %d  \n" , 
	__builtin_types_compatible_p(typeof(a) , typeof(b)) , 
	__builtin_types_compatible_p(typeof(b) , typeof(c)));
	



	return 0 ; 
}
运行结果:

1

0

你可能感兴趣的:(GCC内联函数:__builtin_types_compatible_p)