diff --git a/include/exiv2/slice.hpp b/include/exiv2/slice.hpp index cc45acb8d9..98b5e6c5c4 100644 --- a/include/exiv2/slice.hpp +++ b/include/exiv2/slice.hpp @@ -143,7 +143,7 @@ struct ConstSliceBase : SliceBase { if (new_end > this->end_) { throw std::out_of_range("Invalid input parameters to slice"); } - return slice_type(storage_.data_, new_begin, new_end); + return {storage_.data_, new_begin, new_end}; } protected: @@ -176,21 +176,21 @@ struct MutableSliceBase : public ConstSliceBase { return this->storage_.unsafeAt(this->begin_ + index); } - const value_type& at(size_t index) const { + [[nodiscard]] const value_type& at(size_t index) const { return base_type::at(index); } /*! * Obtain an iterator to the first element in the slice. */ - iterator begin() noexcept { + [[nodiscard]] iterator begin() noexcept { return this->storage_.unsafeGetIteratorAt(this->begin_); } /*! * Obtain an iterator to the first element beyond the slice. */ - iterator end() noexcept { + [[nodiscard]] iterator end() noexcept { return this->storage_.unsafeGetIteratorAt(this->end_); } @@ -227,7 +227,7 @@ struct MutableSliceBase : public ConstSliceBase { * mutable_slice_base. */ template - slice_type subSlice(size_t begin, size_t end) { + [[nodiscard]] slice_type subSlice(size_t begin, size_t end) { this->rangeCheck(begin); // end == size() is a legal value, since end is the first // element beyond the slice @@ -241,7 +241,7 @@ struct MutableSliceBase : public ConstSliceBase { if (new_end > this->end_) { throw std::out_of_range("Invalid input parameters to slice"); } - return slice_type(this->storage_.data_, new_begin, new_end); + return {this->storage_.data_, new_begin, new_end}; } }; @@ -429,24 +429,11 @@ struct Slice : public Internal::MutableSliceBase::type; #endif - /*! - * Construct a sub-slice of this slice with the given bounds. The bounds - * are evaluated with respect to the current slice. - * - * @param[in] begin First element in the new slice. - * @param[in] end First element beyond the new slice. - * - * @throw std::out_of_range when begin or end are invalid - */ - Slice subSlice(size_t begin, size_t end) { - return Internal::MutableSliceBase::template subSlice(begin, end); - } - /*! * Constructs a new constant subSlice. Behaves otherwise exactly like * the non-const version. */ - [[nodiscard]] Slice subSlice(size_t begin, size_t end) const { + Slice subSlice(size_t begin, size_t end) const { return this->to_const_base().template subSlice>(begin, end); } }; @@ -530,7 +517,7 @@ struct Slice : public Internal::MutableSliceBase -Slice makeSlice(T& cont, size_t begin, size_t end) { +[[nodiscard]] Slice makeSlice(T& cont, size_t begin, size_t end) { return {cont, begin, end}; } @@ -538,7 +525,7 @@ Slice makeSlice(T& cont, size_t begin, size_t end) { * Overload of makeSlice for slices of C-arrays. */ template -Slice makeSlice(T* ptr, size_t begin, size_t end) { +[[nodiscard]] Slice makeSlice(T* ptr, size_t begin, size_t end) { return {ptr, begin, end}; } @@ -546,7 +533,7 @@ Slice makeSlice(T* ptr, size_t begin, size_t end) { * @brief Return a new slice spanning the whole container. */ template -Slice makeSlice(container& cont) { +[[nodiscard]] Slice makeSlice(container& cont) { return {cont, 0, cont.size()}; } @@ -555,7 +542,7 @@ Slice makeSlice(container& cont) { * container. */ template -Slice makeSliceFrom(container& cont, size_t begin) { +[[nodiscard]] Slice makeSliceFrom(container& cont, size_t begin) { return {cont, begin, cont.size()}; } @@ -563,7 +550,7 @@ Slice makeSliceFrom(container& cont, size_t begin) { * @brief Return a new slice spanning until `end`. */ template -Slice makeSliceUntil(container& cont, size_t end) { +[[nodiscard]] Slice makeSliceUntil(container& cont, size_t end) { return {cont, 0, end}; } @@ -571,7 +558,7 @@ Slice makeSliceUntil(container& cont, size_t end) { * Overload of makeSliceUntil for pointer based slices. */ template -Slice makeSliceUntil(T* ptr, size_t end) { +[[nodiscard]] Slice makeSliceUntil(T* ptr, size_t end) { return {ptr, 0, end}; } diff --git a/unitTests/test_slice.cpp b/unitTests/test_slice.cpp index a730cfe4c5..36a44cd82a 100644 --- a/unitTests/test_slice.cpp +++ b/unitTests/test_slice.cpp @@ -97,12 +97,11 @@ TYPED_TEST_P(slice, constructionFailsWithZeroLength) { * Test the construction of subSlices and their behavior. */ TYPED_TEST_P(slice, subSliceSuccessfulConstruction) { - using slice_t = Slice; // 0 1 2 3 4 5 6 7 8 9 // | | center_vals // | | middle - slice_t center_vals = this->getTestSlice(3, 7); + auto center_vals = this->getTestSlice(3, 7); ASSERT_EQ(center_vals.size(), static_cast(4)); ASSERT_NO_THROW(center_vals.subSlice(1, 3)); @@ -110,7 +109,7 @@ TYPED_TEST_P(slice, subSliceSuccessfulConstruction) { } TYPED_TEST_P(slice, subSliceFunctions) { - Slice middle = this->getTestSlice(3, 7).subSlice(1, 3); + auto middle = this->getTestSlice(3, 7).subSlice(1, 3); ASSERT_EQ(middle.size(), static_cast(2)); ASSERT_EQ(middle.at(1), static_cast::value_type>(5)); @@ -163,11 +162,9 @@ void checkSubSlice(const Slice& sl) { * Test that all slices can be also passed as const references and still work */ TYPED_TEST_P(slice, constMethodsPreserveConst) { - using slice_t = Slice; - // 0 1 2 3 4 5 6 7 8 9 // | | center_vals - slice_t center_vals = this->getTestSlice(3, 7); + auto center_vals = this->getTestSlice(3, 7); // check at() const works checkConstSliceValueAt(center_vals, 4, 1);