This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: __builtin_types_compatible_p or__is_array() alternative for g++?


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" );


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]