Skip to content

Restore std::array constructors for ArrayRef #7942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion runtime/core/array_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
// removed some implicit const -> non-const conversions that rely on
// complicated std::enable_if meta-programming
// removed a bunch of slice variants for simplicity...
// remove constructors for std::array
// remove constructors and operators for std::vector
// removed some prevention of accidental assignments from temporary that
// required std::enable_if meta-programming
// removed reverse iterator

#pragma once

#include <array>
#include <cstdint>

#include <executorch/runtime/platform/assert.h>
Expand Down Expand Up @@ -87,6 +87,11 @@ class ArrayRef final {
/// Construct a ArrayRef from a range.
ArrayRef(const T* begin, const T* end) : Data(begin), Length(end - begin) {}

/// Construct an ArrayRef from a std::array
template <size_t N>
/* implicit */ constexpr ArrayRef(const std::array<T, N>& Arr)
: Data(Arr.data()), Length(N) {}

/// Construct a ArrayRef from a C array.
template <size_t N>
/* implicit */ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {}
Expand Down Expand Up @@ -202,6 +207,12 @@ ArrayRef<T> makeArrayRef(const T* begin, const T* end) {
return ArrayRef<T>(begin, end);
}

/// Construct an ArrayRef from a std::array.
template <typename T, std::size_t N>
ArrayRef<T> makeArrayRef(const std::array<T, N>& Arr) {
return Arr;
}

/// Construct an ArrayRef from an ArrayRef (no-op) (const)
template <typename T>
ArrayRef<T> makeArrayRef(const ArrayRef<T>& Vec) {
Expand Down
Loading