diff --git a/dune/istl/basearray.hh b/dune/istl/basearray.hh index cd81d6e9a977e97c966f5d36d953340a3e89cbd2..cce76e36242f83ae0d2f8c7575614614c8ddc846 100644 --- a/dune/istl/basearray.hh +++ b/dune/istl/basearray.hh @@ -61,11 +61,16 @@ namespace Imp { //! the type for the index access typedef typename A::size_type size_type; + //! the type used for references + using reference = B&; + + //! the type used for const references + using const_reference = const B&; //===== access to components //! random access to blocks - B& operator[] (size_type i) + reference operator[] (size_type i) { #ifdef DUNE_ISTL_WITH_CHECKING if (i>=n) DUNE_THROW(ISTLError,"index out of range"); @@ -74,7 +79,7 @@ namespace Imp { } //! same for read only access - const B& operator[] (size_type i) const + const_reference operator[] (size_type i) const { #ifdef DUNE_ISTL_WITH_CHECKING if (i>=n) DUNE_THROW(ISTLError,"index out of range"); @@ -147,13 +152,13 @@ namespace Imp { } // Needed for operator[] of the iterator - B& elementAt (std::ptrdiff_t offset) const + reference elementAt (std::ptrdiff_t offset) const { return *(i+offset); } //! dereferencing - B& dereference () const + reference dereference () const { return *i; } @@ -553,10 +558,16 @@ namespace Imp { //! The type used for the index access typedef typename A::size_type size_type; + //! the type used for references + using reference = B&; + + //! the type used for const references + using const_reference = const B&; + //===== access to components //! random access to blocks, assumes ascending ordering - B& operator[] (size_type i) + reference operator[] (size_type i) { const size_type* lb = std::lower_bound(j, j+n, i); if (lb == j+n || *lb != i) @@ -565,7 +576,7 @@ namespace Imp { } //! same for read only access, assumes ascending ordering - const B& operator[] (size_type i) const + const_reference operator[] (size_type i) const { const size_type* lb = std::lower_bound(j, j+n, i); if (lb == j+n || *lb != i) @@ -658,7 +669,7 @@ namespace Imp { } //! dereferencing - B& dereference () const + reference dereference () const { return p[i]; } diff --git a/dune/istl/test/vectortest.hh b/dune/istl/test/vectortest.hh index 1a8538b5fb9c226e0c8aba49a5f1ceefbd08e45d..361ecd3a740e35671389279e0d0358845d351547 100644 --- a/dune/istl/test/vectortest.hh +++ b/dune/istl/test/vectortest.hh @@ -73,6 +73,12 @@ namespace Dune static_assert(std::is_same<typename Vector::ConstIterator, typename Vector::const_iterator>::value, "'ConstIterator' and 'const_iterator' are not the same type"); + // Test reference types + static_assert(std::is_same<typename Vector::reference, typename Vector::reference>::value, + "Vector does not export 'reference'"); + static_assert(std::is_same<typename Vector::const_reference, typename Vector::const_reference>::value, + "Vector does not export 'const_reference'"); + // Test the const_iterator testRandomAccessIterator(v.begin(), v.end(), noop);