]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make sure the conversion only works for 'const' types.
authorWolfgang Bangerth <bangerth@colostate.edu>
Tue, 10 Oct 2023 14:29:53 +0000 (08:29 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 10 Oct 2023 14:29:53 +0000 (08:29 -0600)
include/deal.II/base/array_view.h

index 4cd48526f719e73e899b7539d40227ab0e705f2c..d158f86a7fe46376a6c6687b462b34841d2bba2f 100644 (file)
@@ -222,7 +222,7 @@ public:
    * This constructor allows for cases such as where one has a function
    * that takes an ArrayView object:
    * @code
-   *   void f(const ArrayView<int> &a);
+   *   void f(const ArrayView<const int> &a);
    * @endcode
    * and then to call this function with a list of integers:
    * @code
@@ -233,6 +233,12 @@ public:
    *   f({});
    * @encode
    *
+   * @note This constructor only works if the template type is `const`
+   * qualified. That is, you can initialize an `ArrayView<const int>`
+   * object from a `std::initializer_list<int>`, but not an
+   * `ArrayView<int>` object. This is because the elements of initializer
+   * lists are `const`.
+   *
    * @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
@@ -253,8 +259,9 @@ public:
    *   f(a);
    * @endcode
    */
-  ArrayView(const std::initializer_list<std::remove_cv_t<value_type>>
-              &initializer_list);
+  ArrayView(
+    const std::initializer_list<std::remove_cv_t<value_type>> &initializer_list)
+    DEAL_II_CXX20_REQUIRES(std::is_const_v<ElementType>);
 
   /**
    * Reinitialize a view.
@@ -554,6 +561,7 @@ 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)
+  DEAL_II_CXX20_REQUIRES(std::is_const_v<ElementType>)
   : // use delegating constructor
   ArrayView((initializer.size() > 0 ? initializer.begin() : nullptr),
             initializer.size())

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.