ArrayView, adding ctor for fixed-size views of const(expr) std::array.
This CL allows to reduce the code required to create fixed-size ArrayView
objects for const(expr) std::array instances. Instead of passing .data() and
size(), it is now sufficient to pass the const(expr) std::array instance.
When instancing an array view with variable size, a different ctor is called.
Bug: webrtc:9076
Change-Id: Ie1182fdc33c6b5657f510b6723552813d5933e3e
Reviewed-on: https://webrtc-review.googlesource.com/76820
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23243}
diff --git a/api/array_view.h b/api/array_view.h
index a9df84d..4c1fc86 100644
--- a/api/array_view.h
+++ b/api/array_view.h
@@ -178,9 +178,9 @@
"Array size must match ArrayView size");
}
- // (Only if size is fixed.) Construct an ArrayView with fixed size from an
- // std::array instance. For an ArrayView with variable size, the used ctor is
- // ArrayView(U& u) instead - i.e., the next one.
+ // (Only if size is fixed.) Construct a fixed size ArrayView<T, N> from a
+ // non-const std::array instance. For an ArrayView with variable size, the
+ // used ctor is ArrayView(U& u) instead.
template <typename U,
size_t N,
typename std::enable_if<
@@ -188,6 +188,16 @@
ArrayView(std::array<U, N>& u) // NOLINT
: ArrayView(u.data(), u.size()) {}
+ // (Only if size is fixed.) Construct a fixed size ArrayView<T, N> where T is
+ // const from a const(expr) std::array instance. For an ArrayView with
+ // variable size, the used ctor is ArrayView(U& u) instead.
+ template <typename U,
+ size_t N,
+ typename std::enable_if<
+ Size == static_cast<std::ptrdiff_t>(N)>::type* = nullptr>
+ ArrayView(const std::array<U, N>& u) // NOLINT
+ : ArrayView(u.data(), u.size()) {}
+
// (Only if size is fixed.) Construct an ArrayView from any type U that has a
// static constexpr size() method whose return value is equal to Size, and a
// data() method whose return value converts implicitly to T*. In particular,