From: Peter Munch Date: Mon, 29 Jun 2020 22:16:10 +0000 (+0200) Subject: Enable ArrayView for std::array X-Git-Tag: v9.3.0-rc1~1350^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F10632%2Fhead;p=dealii.git Enable ArrayView for std::array --- diff --git a/doc/news/changes/minor/20200629Munch b/doc/news/changes/minor/20200629Munch new file mode 100644 index 0000000000..7b8ee8d202 --- /dev/null +++ b/doc/news/changes/minor/20200629Munch @@ -0,0 +1,3 @@ +New: The class ArrayView can now also be constructed from std::array. +
+(Peter Munch, 2020/06/29) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 9bf37a5c31..823cc429be 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -168,6 +168,29 @@ public: */ ArrayView(std::vector::type> &vector); + /** + * A constructor that automatically creates a view from a std::array object. + * The view encompasses all elements of the given vector. + * + * This implicit conversion constructor is particularly useful when calling + * a function that takes an ArrayView object as argument, and passing in + * a std::array. + */ + template + ArrayView( + const std::array::type, N> &vector); + + /** + * A constructor that automatically creates a view from a std::array object. + * The view encompasses all elements of the given vector. + * + * This implicit conversion constructor is particularly useful when calling + * a function that takes an ArrayView object as argument, and passing in + * a std::array. + */ + template + ArrayView(std::array::type, N> &vector); + /** * Reinitialize a view. * @@ -422,6 +445,41 @@ inline ArrayView::ArrayView( +template +template +inline ArrayView::ArrayView( + const std::array::type, N> &vector) + : // use delegating constructor + ArrayView(vector.data(), vector.size()) +{ + // the following static_assert is not strictly necessary because, + // if we got a const std::array reference argument but ElementType + // is not itself const, then the call to the forwarding constructor + // above will already have failed: vector.data() will have returned + // a const pointer, but we need a non-const pointer. + // + // nevertheless, leave the static_assert in since it provides a + // more descriptive error message that will simply come after the first + // error produced above + static_assert(std::is_const::value == true, + "This constructor may only be called if the ArrayView " + "object has a const value_type. In other words, you can " + "only create an ArrayView to const values from a const " + "std::array."); +} + + + +template +template +inline ArrayView::ArrayView( + std::array::type, N> &vector) + : // use delegating constructor + ArrayView(vector.data(), vector.size()) +{} + + + template inline bool ArrayView:: diff --git a/tests/base/array_view_16.cc b/tests/base/array_view_16.cc new file mode 100644 index 0000000000..0e432a8618 --- /dev/null +++ b/tests/base/array_view_16.cc @@ -0,0 +1,52 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2015 - 2018 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// test for class ArrayView with std::array + +#include + +#include "../tests.h" + + +void +test() +{ + std::array v; + + ArrayView a(&v[4], 3); // writable view + a[2] = 42; + + Assert(a[2] == 42, ExcInternalError()); + Assert(v[6] == 42, ExcInternalError()); + + ArrayView a2(&v[4], 3); // readable view + Assert(a2[2] == 42, ExcInternalError()); + + ArrayView a3(a); // readable view, converted from 'a' + Assert(a3[2] == 42, ExcInternalError()); + + deallog << "OK" << std::endl; +} + + + +int +main() +{ + initlog(); + + test(); +} diff --git a/tests/base/array_view_16.output b/tests/base/array_view_16.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/array_view_16.output @@ -0,0 +1,2 @@ + +DEAL::OK