ArrayView, adding ctor for fixed-size views of std::array.

This CL allows to reduce the code required to create fixed-size ArrayView
objects for std::array instances. Instead of passing .data() and .size(),
it is now sufficient to pass the std::array instance. When instancing an
array view with variable size, a different ctor is called.

Bug: webrtc:9076
Change-Id: I4fe133b27cd12827ed0206d40184279fc3a196f5
Reviewed-on: https://webrtc-review.googlesource.com/76160
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23220}
diff --git a/api/array_view.h b/api/array_view.h
index d951d0f..a9df84d 100644
--- a/api/array_view.h
+++ b/api/array_view.h
@@ -11,6 +11,7 @@
 #ifndef API_ARRAY_VIEW_H_
 #define API_ARRAY_VIEW_H_
 
+#include <array>
 #include <algorithm>
 #include <type_traits>
 
@@ -169,7 +170,7 @@
     RTC_DCHECK_EQ(0, size);
   }
 
-  // Construct an ArrayView from an array.
+  // Construct an ArrayView from a C-style array.
   template <typename U, size_t N>
   ArrayView(U (&array)[N])  // NOLINT
       : ArrayView(array, N) {
@@ -177,6 +178,16 @@
                   "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.
+  template <typename U,
+            size_t N,
+            typename std::enable_if<
+                Size == static_cast<std::ptrdiff_t>(N)>::type* = nullptr>
+  ArrayView(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,
diff --git a/api/array_view_unittest.cc b/api/array_view_unittest.cc
index 48dff2c..0cea889 100644
--- a/api/array_view_unittest.cc
+++ b/api/array_view_unittest.cc
@@ -9,6 +9,7 @@
  */
 
 #include <algorithm>
+#include <array>
 #include <string>
 #include <utility>
 #include <vector>
@@ -180,6 +181,19 @@
   // v = z;  // Compile error, because can't drop const.
 }
 
+TEST(ArrayViewTest, TestStdArray) {
+  constexpr size_t size = 5;
+  std::array<float, size> arr{};
+  // Fixed size view.
+  rtc::ArrayView<float, size> arr_view_fixed(arr);
+  EXPECT_EQ(arr.data(), arr_view_fixed.data());
+  static_assert(size == arr_view_fixed.size(), "");
+  // Variable size view.
+  rtc::ArrayView<float> arr_view(arr);
+  EXPECT_EQ(arr.data(), arr_view.data());
+  EXPECT_EQ(size, arr_view.size());
+}
+
 TEST(ArrayViewTest, TestStdVector) {
   std::vector<int> v;
   v.push_back(3);