Skip to content

Make the space-optimized adaptor header self-contained - #55

Open
trestoncuzzort wants to merge 1 commit into
boostorg:developfrom
trestoncuzzort:self-contained-space-optimized
Open

trestoncuzzort wants to merge 1 commit into
boostorg:developfrom
trestoncuzzort:self-contained-space-optimized

Conversation

@trestoncuzzort

Copy link
Copy Markdown

Fixes #48.

Including only <boost/circular_buffer/space_optimized.hpp> fails because the adaptor's base class and configuration are not available. Include the main header before the adaptor's include guard. The placement lets the main header include the adaptor while its internal macros are still defined, before it undefines them at the end.

Make the existing adaptor test include this header first, with the existing test hooks enabled. That test fails to compile against the original headers.

Validation:

  • GCC 16.2.1: the full B2 test target passes in C++03 and C++17, including normal/debug adaptor tests: 12 runtime executions and the C++17 benchmark compile check. The benchmark's C++03 configuration is disabled by its Boost.Atomic/Chrono dependencies' C++11 requirements.
  • 32 additional GCC include-order checks pass with -Wall -Wextra -Werror, covering all six permutations of the forward/main/adaptor headers, direct and repeated inclusion, both language modes and both debug settings.
  • Clang 23: direct-header compile/run checks pass in both language modes and debug settings with -Wall -Wextra -Werror.
  • git diff --check passes. Windows was not run locally.

OpenAI Codex assisted with investigation, implementation and verification.

Include the main header before the adaptor guard so its internal macros
remain defined while the adaptor is parsed. Include the adaptor header
first in the existing test to cover standalone inclusion.
@nigels-com

nigels-com commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the patch.
Claude came to a similar conclusion as Codex.
I'm happy to merge this PR as-is, but don't (yet) have permissions to do so.


boost/circular_buffer/space_optimized.hpp doesn't compile when included on its
own, and neither does boost/circular_buffer/base.hpp. Including
space_optimized.hpp alone fails with:

space_optimized.hpp:36: error: expected template-name before '<' token     // circular_buffer<T, Alloc> unknown
space_optimized.hpp:1661: error: 'equal' is not a member of 'std'
space_optimized.hpp:1668: error: 'lexicographical_compare' is not a member of 'std'

The other headers (circular_buffer.hpp, circular_buffer_fwd.hpp,
details.hpp, debug.hpp) compile fine alone.

Cause

The sub-headers were written to be included only through
boost/circular_buffer.hpp. That header does three things around them:

  1. It defines internal macros: BOOST_CB_ENABLE_DEBUG, BOOST_CB_ASSERT,
    BOOST_CB_IS_CONVERTIBLE and BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS.
  2. It includes debug.hpp, details.hpp, base.hpp and space_optimized.hpp,
    in that order.
  3. It #undefs the macros so they don't leak to users.

space_optimized.hpp includes only is_same.hpp and workaround.hpp. It relies
on all of the above, plus circular_buffer<T, Alloc>,
cb_details::capacity_control and the <algorithm>/<iterator> includes,
already being present.

The docs make it worse: doc/circular_buffer.qbk:465–467 says each class "is
defined in" its own header, which invites people to include them directly.

Suggested fix

At the top of both space_optimized.hpp and base.hpp, add an include of the
main header, placed before the header's own include guard:

// Make this header self-contained: <boost/circular_buffer.hpp> defines the
// internal BOOST_CB_* macros, then includes this file.
#include <boost/circular_buffer.hpp>

#if !defined(BOOST_CIRCULAR_BUFFER_SPACE_OPTIMIZED_HPP)
#define BOOST_CIRCULAR_BUFFER_SPACE_OPTIMIZED_HPP
...

Placing it before the guard is what makes this work:

  • If a user includes space_optimized.hpp first, the main header defines the
    macros and then includes space_optimized.hpp again. The guard isn't set yet,
    so the class body is compiled while the macros exist. When control returns to
    the outer copy, its guard is set and it's skipped.
  • If the include went inside the guard, the main header would skip the class,
    #undef the macros, and the body would then be compiled without them.

This keeps the existing design: the macros are still #undef'd, and none leak.
It's two short additions and needs no restructuring.

Alternative considered

Moving the macros into a shared internal header would mean dropping the
#undefs, so the macros would leak into user code. Not recommended.

Verification

The fix was applied to a scratch copy and tested. All of these compile with zero
warnings under -Wall -Wextra and run correctly, in C++03, C++11 and C++20,
with BOOST_CB_ENABLE_DEBUG both 0 and 1:

Includes Result
space_optimized.hpp alone OK
base.hpp alone OK
space_optimized.hpp then circular_buffer.hpp OK
circular_buffer.hpp then space_optimized.hpp OK

Including space_optimized.hpp alone leaves BOOST_CB_ASSERT undefined
afterwards, so no macros leak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

boost/circular_buffer/space_optimized.hpp is not self-contained

2 participants