This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: __builtin_types_compatible_p or__is_array() alternative for g++?
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Eduardo Piombino <drakorg at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Thu, 28 May 2015 10:27:37 +0100
- Subject: Re: __builtin_types_compatible_p or__is_array() alternative for g++?
- Authentication-results: sourceware.org; auth=none
- References: <CAGHqW7_n1i37VxQre7CoyAucOoTskx6UOuN-Bsf7T6u6wXztHQ at mail dot gmail dot com>
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" );