__builtin_types_compatible_p or__is_array() alternative for g++?

Jonathan Wakely jwakely.gcc@gmail.com
Thu May 28 09:27:00 GMT 2015


On 27 May 2015 at 20:29, Eduardo Piombino wrote:
> 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.

You don't need any bultins for C++:

static_assert( std::is_array<decltype(dest)>::value, "dest is an array" );
static_assert( !std::is_array<decltype(p)>::value, "p is not an array" );



More information about the Gcc-help mailing list