如何static_assert某物为数组(包括VLA)?

问题描述 投票:1回答:1

使用GNU扩展。

在C中,我有这个:

#define is_same_type(a, b)                      \
    __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
#define is_array(a)         (!is_same_type((a), &(a)[0]))

#define Static_assert_array(a)                  \
    _Static_assert(is_array(a), "Not a `[]` !")

这正确地静态断言某些东西是数组。

但是,在g ++中,没有__builtin_types_compatible_p()

在g ++中,我有这个:

__builtin_types_compatible_p()

但是当我尝试将其与VLA一起使用时,我发现它不支持VLA:

#define Static_assert_array(a)                  \
    static_assert(std::is_array <__typeof__(a)>::value, "Not a `[]`!")

有什么方法可以使该宏与VLA一起使用?

c++ g++ gnu
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.