https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110167

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The bottom line, however, is that this use of std::to_array is not really
reasonable.

You're asking the compiler to create a variadic pack of 262144 integers, and
then create a pack expansion for std::array<int, 262144>{arr[i]...} where i...
has 262144 elements in the pack.

That's going to be insanely large.

Maybe we can do this though:

  template<typename _Tp, size_t _Nm>
    [[nodiscard]]
    constexpr array<remove_cv_t<_Tp>, _Nm>
    to_array(_Tp (&__a)[_Nm])
    noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
    {
      static_assert(!is_array_v<_Tp>);
      static_assert(is_constructible_v<_Tp, _Tp&>);
      if constexpr (_Nm > 1024 && is_trivially_default_constructible_v<_Tp>
                      && is_trivially_assignable_v<_Tp&, _Tp&>)
        {
          array<_Tp, _Nm> __arr;
          for (size_t __i = 0; __i < _Nm; ++i)
            __arr[__i] = __a[__i];
          return __arr;
        }
      else if constexpr (is_constructible_v<_Tp, _Tp&>)
        return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) {
          return array<remove_cv_t<_Tp>, _Nm>{{ __a[_Idx]... }};
        }(make_index_sequence<_Nm>{});
      __builtin_unreachable(); // FIXME: see PR c++/91388
    }

Reply via email to