]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Allow creation of an ArrayView from a std::initializer_list.
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 9 Oct 2023 23:15:09 +0000 (17:15 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 10 Oct 2023 13:44:43 +0000 (07:44 -0600)
include/deal.II/base/array_view.h

index 684789e32e59114168cb422269f3f9103bd24e2d..4cd48526f719e73e899b7539d40227ab0e705f2c 100644 (file)
@@ -216,6 +216,46 @@ public:
   template <std::size_t N>
   ArrayView(std::array<std::remove_cv_t<value_type>, N> &vector);
 
+  /**
+   * A constructor that creates a view of the array that underlies a
+   * [std::initializer_list](https://en.cppreference.com/w/cpp/utility/initializer_list).
+   * This constructor allows for cases such as where one has a function
+   * that takes an ArrayView object:
+   * @code
+   *   void f(const ArrayView<int> &a);
+   * @endcode
+   * and then to call this function with a list of integers:
+   * @code
+   *   f({1,2,3});
+   * @endcode
+   * This also works with an empty list:
+   * @code
+   *   f({});
+   * @encode
+   *
+   * @note `std::initializer_list` objects are temporary. They are constructed
+   * where the compiler finds a brace-enclosed list, and so they only live
+   * for at most the time it takes to execute the current statement. As a
+   * consequence, creating an ArrayView object of such a `std::initializer_list`
+   * also results in a view object that points to valid memory only for as long
+   * as the current statement is executed. You shouldn't expect that the
+   * resulting ArrayView can be used to point to useful memory content past
+   * that point. In other words, while this code...
+   * @code
+   *   std::vector<int> v(10);
+   *   ArrayView<int> a(v);
+   *   f(a);
+   * @endcode
+   * ...works because the array `v` pointed to exists until after the call to
+   * `f()`, the following code will not likely work as expected:
+   * @code
+   *   ArrayView<int> a({1,2,3});
+   *   f(a);
+   * @endcode
+   */
+  ArrayView(const std::initializer_list<std::remove_cv_t<value_type>>
+              &initializer_list);
+
   /**
    * Reinitialize a view.
    *
@@ -511,6 +551,16 @@ inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
 
 
 
+template <typename ElementType, typename MemorySpaceType>
+inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
+  const std::initializer_list<std::remove_cv_t<value_type>> &initializer)
+  : // use delegating constructor
+  ArrayView((initializer.size() > 0 ? initializer.begin() : nullptr),
+            initializer.size())
+{}
+
+
+
 template <typename ElementType, typename MemorySpaceType>
 inline bool
 ArrayView<ElementType, MemorySpaceType>::operator==(

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.