refactor(cpp): adopt deducing-this pattern - #295
Open
robertodr wants to merge 4 commits into
Open
Conversation
added 2 commits
August 26, 2026 19:00
…educing this MPGraph carried three const/non-const overload pairs whose bodies were identical (active_begin_iterator, active_end_iterator, get_layer), and MPGraphView carried a fourth copy of get_layer_traversal. An explicit object parameter deduces the const-ness instead of declaring it, which also retires the LayerIterator/ ConstLayerIterator alias pair, and a LayerWindow mixin gives both the graph and its views one get_layer_traversal derived from whatever get_layer they expose -- deducing this rather than CRTP, so neither class names itself as a template argument. MonomialPropagator::mp_op()/indexing() duplicated their require_single_partition_ guard across the two overloads, so the guard string could drift. indexing() reaches its result through a unique_ptr, whose operator* yields a mutable referent whatever the owner's const-ness, so it needs std::forward_like to stay const-correct; new static_asserts pin that down, since nothing else observed it. Assisted-by: ClaudeCode:claude-opus-5
Bitset::data() and PartitionGroup::partition() were const/non-const overload pairs with one body each; partition() dereferences a unique_ptr, so it needs std::forward_like to keep the group's const-ness. The bitwise operators keep their hidden-friend form on purpose. Taking the object parameter by value would make it the working copy and shorten each to one line, but a by-value object parameter is a stack array: NRVO no longer applies, and under -fstack-protector-strong (the platform default) GCC 15 grows operator^ from four instructions to twelve, adding a frame and a canary check to the library's hottest primitive. Recorded in a comment there and as a rule in AGENTS.md, so the next reader does not re-derive it. Assisted-by: ClaudeCode:claude-opus-5
robertodr
requested review from
diagonal-hamiltonian,
fpietra and
ludmilaasb
as code owners
August 26, 2026 19:10
|
Docs preview: https://pr-295.monoprop-docs.pages.dev |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #295 +/- ##
=======================================
Coverage 97.70% 97.70%
=======================================
Files 14 14
Lines 742 742
Branches 98 98
=======================================
Hits 725 725
Misses 12 12
Partials 5 5
Flags with carried forward coverage won't be shown. Click here to find out more. |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
🤖 AI text below 🤖
This pull request introduces a significant refactor to the C++ codebase, adopting C++23 "deducing this" member functions to unify const and non-const accessors, reduce code duplication, and improve maintainability. The changes affect several core classes, including
MPGraph,MonomialPropagator,Bitset, and related test files, and also introduce a newLayerWindowmixin to share accessor logic. Additionally, the documentation and test assertions are updated to reflect and verify the new idioms.Adoption of C++23 "deducing this" idiom and accessor refactoring:
Refactored multiple accessor pairs (const/non-const) in
MPGraph,MonomialPropagator,Bitset, andPartitionGroupto single deducing-this member functions, ensuring the returned type's const-ness matches the object and reducing code duplication. This includes methods likeget_layer,mp_op,indexing,partition, anddata[1]](https://github.com/yourrepo/commit/F3345933#diff-L41R40), [2]](https://github.com/yourrepo/commit/F61b741e#diff-L116R116), [3]](https://github.com/yourrepo/commit/F61b741e#diff-L157R156), [4]](https://github.com/yourrepo/commit/F0c67259#diff-L177R181), [5]](https://github.com/yourrepo/commit/Fc93b6da#diff-L106R107)).Introduced the
LayerWindowmixin struct to encapsulate shared layer accessor logic, allowing bothMPGraphandMPGraphViewto inherit and use deducing-this accessors for layer traversal ([cpp/monoprop/detail/graph/MPGraphViews.hR71-R83](https://github.com/yourrepo/commit/Ffaa68d4#diff-L68R68)).Documentation and style guide updates:
AGENTS.mdto recommend deducing-this members for accessor pairs and clarified best practices regarding object parameter passing and NRVO (Named Return Value Optimization) ([AGENTS.mdL173-R183](https://github.com/yourrepo/commit/Fc21d54b#diff-L170R170)).Bitsetand other classes to clarify the rationale behind deducing-this usage and parameter passing choices ([cpp/monoprop/Bitset.hR112-R115](https://github.com/yourrepo/commit/F0c67259#diff-L109R109)).Test and assertion improvements:
General codebase improvements:
<utility>,std::forward_like) to support the new idioms ([cpp/monoprop/detail/partition/PartitionGroup.hR29](https://github.com/yourrepo/commit/Fc93b6da#diff-L26R26)).These changes modernize the codebase, reduce maintenance burden, and ensure safer and clearer accessor patterns throughout the project.
Checklist
docs/,CONTRIBUTING.md) if neededCHANGELOG/ release notes updated if applicableAI/LLM disclosure
Important
By opening this PR I confirm that I have read CONTRIBUTING.md and I agree to the terms of the Contributor License Agreement.
Warning
If you're contributing on behalf of your employer, contact cla@algorithmiq.fi to arrange a Corporate CLA.