[Main]
Title=__builtin_types_compatible_p

[Top]
<CODE><B><A HREF="$$INFOLINK(keywords/int)">int</A></B> __builtin_types_compatible_p (type1, type2);</CODE>
<BR><BR>
You can use the built-in function __builtin_types_compatible_p to
determine whether two types are the same.
<BR><BR>
This built-in function returns 1 if the unqualified versions of the
types <I>type1</I> and <I>type2</I> (which are types, not expressions) are
compatible, 0 otherwise.  The result of this built-in function can be
used in integer constant expressions.
<BR><BR>
This built-in function ignores top level qualifiers (<CODE><A HREF="$$INFOLINK(keywords/const)">const</A></CODE>,
<CODE><A HREF="$$INFOLINK(keywords/volatile)">volatile</A></CODE>, etc.).  For example, <CODE><A HREF="$$INFOLINK(keywords/int)">int</A></CODE> is equivalent to <CODE><A HREF="$$INFOLINK(keywords/const)">const</A>
<A HREF="$$INFOLINK(keywords/int)">int</A></CODE>.
<BR><BR>
The type <CODE><A HREF="$$INFOLINK(keywords/int)">int</A>[]</CODE> and <CODE><A HREF="$$INFOLINK(keywords/int)">int</A>[5]</CODE> are compatible.  On the other
hand, <CODE><A HREF="$$INFOLINK(keywords/long)">long</A>&nbsp;<A HREF="$$INFOLINK(keywords/int)">int</A></CODE> and <CODE><A HREF="$$INFOLINK(keywords/char)">char</A>*</CODE> are not compatible,
although their sizes are the same.  Also, the
amount of pointer indirection is taken into account when determining
similarity.  Consequently, <CODE><A HREF="$$INFOLINK(keywords/short)">short</A>*</CODE> is not similar to
<CODE><A HREF="$$INFOLINK(keywords/short)">short</A>**</CODE>.  Furthermore, two types that are typedefed are
considered compatible if their underlying types are compatible.
<BR><BR>
An <CODE><A HREF="$$INFOLINK(keywords/enum)">enum</A></CODE> type is considered to be compatible with another
<CODE><A HREF="$$INFOLINK(keywords/enum)">enum</A></CODE> type.  For example, <CODE>enum&nbsp;{foo,&nbsp;bar}</CODE> is similar to
<CODE>enum&nbsp;{hot,&nbsp;dog}</CODE>.
<BR><BR>
You would typically use this function in code whose execution varies
depending on the arguments' types.  For example:
<PRE>#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;                                                        \
  })
</PRE>
