Skip to content

register classes by reflection in C++26 - #94

Open
jll63 wants to merge 1 commit into
boostorg:developfrom
jll63:feature/reflection-registration
Open

register classes by reflection in C++26#94
jll63 wants to merge 1 commit into
boostorg:developfrom
jll63:feature/reflection-registration

Conversation

@jll63

@jll63 jll63 commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Closes #89.

When the compiler supports C++26 reflection (P2996), the library can find the
classes taking part in dispatch on its own, and BOOST_OPENMETHOD_CLASSES
becomes unnecessary in most cases.

What's new

  • use_classes_in<^^Namespace, Registry> (core.hpp) — a registrar that
    scans a namespace by reflection for the classes involved in dispatch, plus
    their bases, and registers them. Virtual and multiple inheritance are
    supported; unlike use_classes, repeated inheritance is not an error here —
    an ambiguous base cannot take part in dispatch, so it is left out.
  • BOOST_OPENMETHOD_CLASSES_IN(NAMESPACE, ...) (macros.hpp) — the macro
    wrapper. Reflection sees only what precedes it, so it goes at the bottom of
    the file. Without reflection it expands to nothing, so a file that also calls
    BOOST_OPENMETHOD_CLASSES builds under either standard.
  • policies::explicit_class_registration (preamble.hpp) — opts a registry
    out, restoring the C++17 behaviour where every class must be registered by
    hand. The registry exposes has_reflected_class_registration. The policy has
    no effect if the compiler does not support reflection.

Methods are found through the names that denote them — the using declaration
BOOST_OPENMETHOD now emits, a hand-written one, or any registrar object. A
core-interface method whose method<...> type is spelled out in full at every
use, with neither a using declaration nor an overrider, is named by nothing
and is not found; its classes still need use_classes.

Detection is automatic, from __cpp_impl_reflection — nothing about the C++17
build changes.

Tests

test_classes.hpp adds BOOST_OPENMETHOD_TEST_CLASSES, which expands to
BOOST_OPENMETHOD_CLASSES in C++17 and to nothing under reflection. Tests that
are not about class registration use it and add a trailing
BOOST_OPENMETHOD_CLASSES_IN(::), so a C++26 run exercises reflection-based
registration across the whole suite: the classes go unregistered and every test
still has to pass. Tests that check what happens when a class is not
registered keep BOOST_OPENMETHOD_CLASSES and put
explicit_class_registration in their registry.

test_reflection.cpp covers the scan itself.

Build

  • CMake: BOOST_OPENMETHOD_ENABLE_REFLECTION=ON probes for -std=c++26 and
    -std=c++26 -freflection, and applies whichever works per target (not through
    CMAKE_CXX_FLAGS — CMake probes the compiler before CMAKE_CXX_STANDARD
    takes effect, and GCC rejects -freflection under any other standard).
  • b2: <toolset>gcc,<cxxstd>26:<cxxflags>-freflection.
  • CI: a new reflection job builds and runs the suite with GCC 16.

Verified locally in both configurations: C++17/clang 153/153, and C++26
reflection (g++-16 -freflection) 148/148.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@cppalliance-bot

Copy link
Copy Markdown

An automated preview of the documentation is available at https://94.openmethod.prtest3.cppalliance.org/libs/openmethod/doc/html/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-08-25 15:16:19 UTC

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

auto-register classes in C++26

2 participants