From 3d9436416920b87ca0139d70ccba2ed8689b966a Mon Sep 17 00:00:00 2001 From: Andrzej Krzemienski Date: Thu, 26 Nov 2026 01:30:34 +0100 Subject: [PATCH 1/3] Refine concept ConstBufferSequence Concept ConstBufferSequence has been added the copy_constructible requirement which it was missing. This is both syntactic and semantic requirements. Refined the documentation of the concepts as well. --- .../ROOT/pages/5.buffers/5e.rationale.adoc | 13 ++ include/boost/capy/buffers.hpp | 115 ++++++++++++------ include/boost/capy/detail/buffer_array.hpp | 4 +- test/unit/buffers/buffer.cpp | 24 ++-- 4 files changed, 107 insertions(+), 49 deletions(-) diff --git a/doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc b/doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc index 7211c6c81..e6ce51f61 100644 --- a/doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc +++ b/doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc @@ -11,6 +11,19 @@ = Rationale :page-mode: explanation +== Why not cpp:span[std::span]? + +The C++ Standard Library already provides a type for representing a handle +to a contiguous memory region: cpp:std::span[]. Why create a new type cpp:const_buffer[]? +The answer is in undesired implicit conversions that cpp:std::span[] offers. + +One of the important guarantees that concepts cpp:ConstBuffer[] and cpp:ConstBufferSequence[] offer +is that copying their models does not change the location of the contiguous memory region(s). +cpp:std::span[] is very liberal about allowing implicit conversions. If we used it, +it would make cpp:std::vector[] satisfy cpp:ConstBuffer[] without +meeting its semantic requirements (shallow copy), and thus causing the programs that rely on these +semantic requirements to behave incorrectly. + == Why `void*` rather than `std::byte`? Two concrete forces decide it: diff --git a/include/boost/capy/buffers.hpp b/include/boost/capy/buffers.hpp index 5d93afb11..952a02eef 100644 --- a/include/boost/capy/buffers.hpp +++ b/include/boost/capy/buffers.hpp @@ -2,6 +2,7 @@ // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) // Copyright (c) 2026 Michael Vandeberg // Copyright (c) 2026 Steve Gerbino +// Copyright (c) 2026 Andrzej Krzemieński // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -228,18 +229,23 @@ class const_buffer } }; -/** Requires a type to convert to `const_buffer`. +/** Type recognized by the library as a _buffer_ (a handle to contiguous memory region) + with const access to data. - This is the element requirement of @ref ConstBufferSequence. + @par Semantic Requirements - For a `T const` lvalue `x`, both `const_buffer b = x;` and - `const_buffer(x)` must be valid, since algorithms convert elements - through a const reference and may use either form. A conversion - operator must therefore be `const`. + After the conversion to @ref const_buffer `b`, `b.data()` and `b.size()` + designate a valid contiguous memory region. The region shall remain valid + for the duration of an operation represented by the concept-constrained + function template call. - @note Algorithms that read buffer sequences are `noexcept`, so a - conversion that throws terminates the program. Conversion operators - should be `noexcept`. + @note Function templates in this library constrained by this concept, + such as @ref buffer_size, are often `noexcept`, so if this conversion + throws, this results in calling `std::terminate()`. + + @note `std::convertible_to` implies that @ref const_buffer + can also be explicitly constructed from `T const&` with the same effect as the + implicit conversion. @see @ref const_buffer, @ref MutableBuffer */ @@ -247,17 +253,23 @@ template concept ConstBuffer = std::convertible_to; -/** Requires a type to convert to both `mutable_buffer` and `const_buffer`. +/** Type recognized by the library as a _buffer_ (a handle to contiguous memory region) + with mutable access to data. + + @par Semantic Requirements - This is the element requirement of @ref MutableBufferSequence. + After the conversion to @ref mutable_buffer or @ref const_buffer `b`, `b.data()` and `b.size()` + designate a valid contiguous memory region. The region shall remain valid + for the duration of an operation represented by the concept-constrained + function template call. - A type that converts only to `mutable_buffer` does not qualify. - Reaching `const_buffer` from it takes two user-defined conversions, - and an implicit conversion sequence allows one. Such a type must - also provide a conversion to `const_buffer`. + @note Function templates in this library constrained by this concept, + such as @ref buffer_size, are often `noexcept`, so if this conversion + throws, this results in calling `std::terminate()`. - As with @ref ConstBuffer, the conversion is from a `T const` lvalue, - and conversion operators should be `noexcept`. + @note `std::convertible_to` implies that @ref mutable_buffer + can also be explicitly constructed from `T const&` with the same effect as the + implicit conversion. @see @ref mutable_buffer, @ref ConstBuffer */ @@ -265,42 +277,65 @@ template concept MutableBuffer = ConstBuffer && std::convertible_to; -/** Requires a type to be a @ref ConstBuffer, or a range of them. - - A type satisfies `ConstBufferSequence` if it represents one or more - contiguous memory regions that can be read. This includes single - buffers and ranges of buffers. - - @par Syntactic Requirements - @li @ref ConstBuffer, OR - @li A bidirectional range whose value type is a @ref ConstBuffer +/** Type recognized by the library as a _sequence_ of buffers with const access to data: + either a type modelling @ref ConstBuffer or a range thereof. + @par Semantic Requirements + + A copy-constructed buffer sequence shall refer to the same contiguous memory regions as the original. + This can be expressed as: + + @code + bool copy_postcondition(ConstBufferSequence auto const& x) + { + ConstBufferSequence auto y = x; + return std::equal( + buffer_sequence_begin(x), buffer_sequence_end(x), + buffer_sequence_begin(y), buffer_sequence_end(y), + [](const const_buffer& bx, const const_buffer& by) + { + return bx.data() == by.data() + && bx.size() == by.size(); + } + ); + } + @endcode + + If a type models both @ref ConstBuffer and a range thereof, + then it is treated as @ref ConstBuffer. + + @note Function templates in this library constrained by this concept, + such as @ref buffer_size, are often `noexcept`, so if the operations used + to iterate over the buffer range -- such as `.begin()` or the iterator API -- + throw, this results in calling `std::terminate()`. + @see @ref const_buffer, @ref MutableBufferSequence */ // tag::const_buffer_sequence_concept[] template concept ConstBufferSequence = + std::copy_constructible && ( ConstBuffer || ( std::ranges::bidirectional_range && - ConstBuffer>); + ConstBuffer>)); // end::const_buffer_sequence_concept[] -/** Requires a type to be a @ref MutableBuffer, or a range of them. - - A type satisfies `MutableBufferSequence` if it represents one or more - contiguous memory regions that can be written. This includes single - buffers and ranges of buffers. - - Every `MutableBufferSequence` is also a @ref ConstBufferSequence. +/** Type recognized by the library as a _sequence_ of buffers with mutable access to data: + either a type modelling @ref MutableBuffer or a range thereof. - A @ref ConstBuffer is a single buffer, even if it is also a range, - so only its own conversion to `mutable_buffer` is considered. + @par Semantic Requirements + + If a type models both @ref MutableBuffer and a range thereof, + then it is treated as @ref MutableBuffer. - @par Syntactic Requirements - @li @ref MutableBuffer, OR - @li A bidirectional range, not a @ref ConstBuffer, whose value type - is a @ref MutableBuffer + @note A type satisfying @ref ConstBuffer is never considered to be a range of + mutable buffers. + @note Function templates in this library constrained by this concept, + such as @ref buffer_size, are often `noexcept`, so if the operations used + to iterate over the buffer range — such as `.begin()` or the iterator API — + throw, this results in calling `std::terminate()`. + @see @ref mutable_buffer, @ref ConstBufferSequence */ // tag::mutable_buffer_sequence_concept[] diff --git a/include/boost/capy/detail/buffer_array.hpp b/include/boost/capy/detail/buffer_array.hpp index 4691042ff..8e863c25a 100644 --- a/include/boost/capy/detail/buffer_array.hpp +++ b/include/boost/capy/detail/buffer_array.hpp @@ -103,9 +103,9 @@ class buffer_array @param bs The buffer sequence to copy from. */ template - requires (IsConst ? ConstBufferSequence : MutableBufferSequence) - && (!std::same_as, buffer_array>) + requires (!std::same_as, buffer_array>) && (!std::same_as, value_type>) + && (IsConst ? ConstBufferSequence : MutableBufferSequence) buffer_array(BS const& bs) noexcept : dummy_(0) { diff --git a/test/unit/buffers/buffer.cpp b/test/unit/buffers/buffer.cpp index 87f64a955..1025f80d9 100644 --- a/test/unit/buffers/buffer.cpp +++ b/test/unit/buffers/buffer.cpp @@ -50,10 +50,10 @@ static_assert( ConstBufferSequence>); static_assert(! MutableBufferSequence>); static_assert( MutableBufferSequence>); -static_assert( ConstBufferSequence); -static_assert( ConstBufferSequence); +static_assert(! ConstBufferSequence); +static_assert(! ConstBufferSequence); static_assert(! MutableBufferSequence); -static_assert( MutableBufferSequence); +static_assert(! MutableBufferSequence); // User types with conversion operators @@ -111,6 +111,18 @@ struct user_buffer operator mutable_buffer() const noexcept { return { p, n }; } operator const_buffer() const noexcept { return { p, n }; } + + + // The following is to have API without default constructors or copy/move assignments. + // this is to make sure that constrained templates use only the API advertised + // by the concepts. + + struct init_t {}; + user_buffer(init_t, char* p_, std::size_t n_) : p{p_}, n{n_} {} + user_buffer(user_buffer&&) = default; + user_buffer(user_buffer const&) = default; + user_buffer& operator=(user_buffer&&) = delete; + user_buffer& operator=(user_buffer const&) = delete; }; } // namespace @@ -456,8 +468,6 @@ struct buffer_test testBuffer>(); testBuffer>(); testBuffer>(); - testBuffer(); - testBuffer(); } //-------------------------------------------- @@ -611,11 +621,11 @@ struct buffer_test void testUserBuffer() { char data[8] = {}; - user_buffer const ub{ data, 4 }; + user_buffer const ub{ user_buffer::init_t{}, data, 4 }; BOOST_TEST_EQ(buffer_size(ub), 4); std::array const arr{{ - { data, 4 }, { data + 4, 4 } }}; + { user_buffer::init_t{}, data, 4 }, { user_buffer::init_t{}, data + 4, 4 } }}; BOOST_TEST_EQ(buffer_size(arr), 8); char const src[] = "abcdefgh"; From 7856144dc4d1002d4f9a251c3cacd49969682a93 Mon Sep 17 00:00:00 2001 From: Andrzej Krzemienski Date: Sun, 29 Nov 2026 22:33:47 +0100 Subject: [PATCH 2/3] docs: Fix the semantic requirements of ConstBufferSequence Now making sure that the semantic requirements of ConstBufferSequence also include ranges represented by iterator+sentinel. --- include/boost/capy/buffers.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/capy/buffers.hpp b/include/boost/capy/buffers.hpp index 952a02eef..308645195 100644 --- a/include/boost/capy/buffers.hpp +++ b/include/boost/capy/buffers.hpp @@ -289,7 +289,7 @@ concept MutableBuffer = bool copy_postcondition(ConstBufferSequence auto const& x) { ConstBufferSequence auto y = x; - return std::equal( + return std::ranges::equal( buffer_sequence_begin(x), buffer_sequence_end(x), buffer_sequence_begin(y), buffer_sequence_end(y), [](const const_buffer& bx, const const_buffer& by) From 6f59eb6884038d3b781c5c8062dcefa2dccf3bf8 Mon Sep 17 00:00:00 2001 From: Andrzej Krzemienski Date: Tue, 1 Dec 2026 23:24:41 +0100 Subject: [PATCH 3/3] Reorder constraints in ConstBufferSequence to minimize recursion errors This commit also applies minor documentaiton fixes. --- include/boost/capy/buffers.hpp | 51 ++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/include/boost/capy/buffers.hpp b/include/boost/capy/buffers.hpp index 308645195..66c0478eb 100644 --- a/include/boost/capy/buffers.hpp +++ b/include/boost/capy/buffers.hpp @@ -229,19 +229,18 @@ class const_buffer } }; -/** Type recognized by the library as a _buffer_ (a handle to contiguous memory region) - with const access to data. +/** Type recognized by the library as a _buffer_ (a handle to contiguous memory region) with const access to data. - @par Semantic Requirements + @par Semantic Requirements - After the conversion to @ref const_buffer `b`, `b.data()` and `b.size()` + After the conversion to @ref const_buffer `b`, `b.data()` and `b.size()` designate a valid contiguous memory region. The region shall remain valid for the duration of an operation represented by the concept-constrained function template call. @note Function templates in this library constrained by this concept, such as @ref buffer_size, are often `noexcept`, so if this conversion - throws, this results in calling `std::terminate()`. + throws, this results in calling `std::terminate()`. @note `std::convertible_to` implies that @ref const_buffer can also be explicitly constructed from `T const&` with the same effect as the @@ -253,38 +252,40 @@ template concept ConstBuffer = std::convertible_to; -/** Type recognized by the library as a _buffer_ (a handle to contiguous memory region) - with mutable access to data. +/** Type recognized by the library as a _buffer_ (a handle to contiguous memory region) with mutable access to data. - @par Semantic Requirements + @par Semantic Requirements - After the conversion to @ref mutable_buffer or @ref const_buffer `b`, `b.data()` and `b.size()` + After the conversion to @ref mutable_buffer or @ref const_buffer `b`, `b.data()` and `b.size()` designate a valid contiguous memory region. The region shall remain valid for the duration of an operation represented by the concept-constrained function template call. @note Function templates in this library constrained by this concept, such as @ref buffer_size, are often `noexcept`, so if this conversion - throws, this results in calling `std::terminate()`. + throws, this results in calling `std::terminate()`. @note `std::convertible_to` implies that @ref mutable_buffer can also be explicitly constructed from `T const&` with the same effect as the implicit conversion. + @note A type that has a non-explicit conversion operator to `mutable_buffer` + does not automatically satisfy the requirement of being convertible to + `const_buffer`. + @see @ref mutable_buffer, @ref ConstBuffer */ template concept MutableBuffer = ConstBuffer && std::convertible_to; -/** Type recognized by the library as a _sequence_ of buffers with const access to data: - either a type modelling @ref ConstBuffer or a range thereof. +/** Type recognized by the library as a _sequence_ of buffers with const access to data: either a type modelling @ref ConstBuffer or a range thereof. @par Semantic Requirements - + A copy-constructed buffer sequence shall refer to the same contiguous memory regions as the original. This can be expressed as: - + @code bool copy_postcondition(ConstBufferSequence auto const& x) { @@ -298,33 +299,35 @@ concept MutableBuffer = && bx.size() == by.size(); } ); - } + } @endcode + A move-constructed buffer sequence shall refer to the same contiguous memory regions + as the original did prior to the move. + If a type models both @ref ConstBuffer and a range thereof, then it is treated as @ref ConstBuffer. @note Function templates in this library constrained by this concept, such as @ref buffer_size, are often `noexcept`, so if the operations used - to iterate over the buffer range -- such as `.begin()` or the iterator API -- + to iterate over the buffer range — such as `.begin()` or the iterator API — throw, this results in calling `std::terminate()`. - + @see @ref const_buffer, @ref MutableBufferSequence */ // tag::const_buffer_sequence_concept[] template concept ConstBufferSequence = - std::copy_constructible && ( - ConstBuffer || ( + (ConstBuffer || ( std::ranges::bidirectional_range && - ConstBuffer>)); + ConstBuffer>)) + && std::copy_constructible; // end::const_buffer_sequence_concept[] -/** Type recognized by the library as a _sequence_ of buffers with mutable access to data: - either a type modelling @ref MutableBuffer or a range thereof. +/** Type recognized by the library as a _sequence_ of buffers with mutable access to data: either a type modelling @ref MutableBuffer or a range thereof. @par Semantic Requirements - + If a type models both @ref MutableBuffer and a range thereof, then it is treated as @ref MutableBuffer. @@ -335,7 +338,7 @@ concept ConstBufferSequence = such as @ref buffer_size, are often `noexcept`, so if the operations used to iterate over the buffer range — such as `.begin()` or the iterator API — throw, this results in calling `std::terminate()`. - + @see @ref mutable_buffer, @ref ConstBufferSequence */ // tag::mutable_buffer_sequence_concept[]