llvm / llvm/llvm-project

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

Open
#173,949 1 comment 0 reactions 0 assignees View on GitHub
clang:frontend constexpr
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Consider the following code

```cpp
#include
#include

template
inline auto constexpr MakeConstexprArray()
{
auto result = f();
return (([&](std::index_sequence)
{
return std::array{result[Indices]...};
})(std::make_index_sequence()));
}

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

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{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{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.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the provided C++23 source and clang++ command, testing both 32768 and 32769 elements. Trace the compiler handling of the constexpr std::array aggregate initialization and identify why the diagnostics change at that boundary. Done means the valid example no longer produces these initializer errors without regressing smaller arrays.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.