Skip to content

error: initializer for aggregate with no elements requires explicit braces or error: excess elements in struct initializer in clang++ with more than 32767 elements #173949

@user202729

Description

@user202729

Consider the following code

#include<ranges>
#include<vector>

template <auto f>
inline auto constexpr MakeConstexprArray()
{
    auto result = f();
    return (([&]<std::size_t... Indices>(std::index_sequence<Indices...>) 
      {
          return std::array<typename decltype(result)::value_type, sizeof...(Indices)>{result[Indices]...};
      })(std::make_index_sequence<f().size()>()));
}

static constexpr auto x = MakeConstexprArray<[]{ return std::views::iota(0, 32768) | std::ranges::to<std::vector>(); }>();

int main() {}

Compile with clang++ -fconstexpr-steps=1271242 -std=c++23 a.cpp, I get

a.cpp:10:88: error: initializer for aggregate with no elements requires
      explicit braces
   10 |           return std::array<typename decltype(result)::value_type, sizeof...(Indices)>{resul
...
      |                                                                                        ^
a.cpp:8:13: note: in instantiation of function template specialization

if I change 32768 to 32769, I get

a.cpp:10:88: error: excess elements in struct initializer
   10 |   ...std::array<typename decltype(result)::value_type, sizeof...(Indices)>{result[Indices]..
.};
      |                                                                            ^~~~~~~~~~~~~~~
a.cpp:8:13: note: in instantiation of function template specialization

on the other hand, we probably don't want to slow down code that uses ≤ 32767 elements by using a larger data type or bound-check every addition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions