首页 > 其他 > 详细

GCC内联函数:__builtin_types_compatible_p

时间:2016-07-06 15:01:26      阅读:329      评论:0      收藏:0      [点我收藏+]
#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

原文:http://blog.csdn.net/morixinguan/article/details/51837547

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!