Skip to content

Aggregate initialization of struct containing zero-sized array disagrees with GCC #35489

@Quuxplusone

Description

@Quuxplusone
Bugzilla Link 36141
Version trunk
OS All
CC @DougGregor,@zygoloid

Extended Description

Clang believes that an aggregate consisting of a zero-sized array and a 1-sized array is NOT initializable with just one initializer in the curly braces, whereas GCC believes that it IS.

prog.cc:15:16: error: initializer for aggregate with no elements requires explicit braces
    auto x = A{convertible_to_anything{}};
               ^

It would be convenient if Clang matched GCC's behavior for zero-sized arrays. The existing behavior is definitely not a "bug bug", as zero-sized arrays are non-standard and I'm not aware that Clang has promised to mimic GCC in this area; but the discrepancy between the two compilers in this area is awkward for testing.

#include <cstdio>
#include <type_traits>
#include <utility>

struct convertible_to_anything { template<class T> constexpr operator T&() const noexcept; };
template<class T, class, class> struct is_n_constructible_impl : std::false_type {};
template<class T, size_t... Is> struct is_n_constructible_impl<T, std::index_sequence<Is...>, decltype(void(T{(void(Is), convertible_to_anything{})...}))> : std::true_type {};

template<size_t N, class T> struct is_n_constructible : is_n_constructible_impl<T, std::make_index_sequence<N>, void> {};


struct A { int a[0]; int b[1]; };

int main() {
    static_assert(is_n_constructible<0, A>::value);
    if (is_n_constructible<1, A>::value) {
        puts("This is GCC");
    } else {
        puts("This is Clang");
    }
    static_assert(not is_n_constructible<2, A>::value);
    static_assert(not is_n_constructible<3, A>::value);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++clang:frontendLanguage frontend issues, e.g. anything involving "Sema"invalidResolved as invalid, i.e. not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions