__builtin_types_compatible_p or__is_array() alternative for g++?

Eduardo Piombino drakorg@gmail.com
Wed May 27 19:29:00 GMT 2015


Hi.

I'm currently using __builtin_types_compatible_p basically for
determining if the passed value is an array in the following manner:

#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
#define __is_array(a) (__same_type((a), &(a)[0]) == 0)

This way the __is_array() tells me if im dealing with an array or a
bare pointer:

char dest[10];
char * p;

__is_array(dest) would evaluate to 1, while __is_array(p) would
evaluate to 0, allowing me to raise a warning or an assert at compile
time.

However, when trying to do the same in g++, I don't have that builtin,
and despite having tried many other alternatives I haven't been able
to come up with a macro that would solve this and work for every case
as the C version:

So far, when compiling g++, I'm using this approach::

#define __is_indexable(arg) (sizeof((arg)[0]))
#define __is_array(arg) (__is_indexable((arg)) && (((void *) &(arg))
== ((void *) (arg))))

which works well for the main cases:
__is_array(dest) and __is_array(p), but fails to compile on
__is_array(p++), _is_array(p + 1) or _is_array(&dest[0]), which is
also used quite extensively across the existing source code. (the C
version of the macros work well on all of those cases)

So, my question is: is there any equivalent for the
__builtin_types_compatible_p available when compiling c++ code? Or if
not at all, do you know any other approach that could allow me to test
against (p++, or &dest[0]) without breaking compilation?

Thank you,
Best regards,
Eduardo.



More information about the Gcc-help mailing list