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.