__builtin_types_compatible_p

Previous Other built-in functions provided by GCC Next

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 (const, volatile, etc.). For example, int is equivalent to const int.

The type int[] and int[5] are compatible. On the other hand, long int and char* are not compatible, although their sizes 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 considered to be compatible with another enum type. For example, enum {foo, bar} is 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;                                                        \
  })