[common] enforce correct CRTP usage for mix-in classes - #11880
Conversation
Library files
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11880 +/- ##
==========================================
+ Coverage 74.25% 74.64% +0.39%
==========================================
Files 643 645 +2
Lines 100095 95803 -4292
==========================================
- Hits 74323 71515 -2808
+ Misses 25772 24288 -1484
🚀 New features to boost your workflow:
|
023ef43 to
32a7534
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request correctly enforces CRTP usage for the mix-in helper classes by making their constructors private and friending the derived type. This is a great change that will prevent subtle bugs from incorrect inheritance by catching them at compile time. The refactoring of Equatable<T> to remove its dependency on Unequatable<T> is also a clean improvement. The changes are logical and well-executed. I have one minor suggestion to improve a comment for better clarity.
The mix-in helper classes like `Clearable<T>`, `Equatable<T>`, and `Unequatable<T>` are intended for CRTP style inheritance, where `T` is the derived class itself. A mistaken inheritance, such as `class Foo : public Clearable<Bar>`, can compile successfully but lead to subtle bugs. This change enforces the correct CRTP usage at compile time. By making the constructors of these helper classes `private` and declaring the derived template class `T` as a `friend`, any incorrect inheritance will now result in a build failure. This approach correctly detects such a mistake, even if `Foo` and `Bar` happen to be `friend`s of each other. Additionally, `Equatable<T>` is updated to provide both `operator==` and `operator!=`, removing its dependency on `Unequatable<T>`. This change allows us to apply the `private` constructor enforcement to `Equatable<T>` as well.
32a7534 to
ff003b4
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR enforces correct CRTP (Curiously Recurring Template Pattern) usage for mix-in helper classes by making their constructors private and using friend declarations. This prevents compile-time errors from incorrect inheritance patterns where the template parameter doesn't match the derived class.
Key changes:
- Added private constructors and friend declarations to
Clearable<T>,Equatable<T>, andUnequatable<T> - Removed
Equatable<T>'s inheritance fromUnequatable<T>and implementedoperator!=directly - Enhanced compile-time safety for CRTP pattern enforcement
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/core/common/equatable.hpp | Added CRTP enforcement to Unequatable<T> and Equatable<T>, removed inheritance relationship |
| src/core/common/clearable.hpp | Added CRTP enforcement to Clearable<T> with private constructor and friend declaration |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
Is there another mistaken inheritance case that a class having the Clearable mix-in is inherited by another class? It which would cause the similar problem the PR is trying to prevent, right? |
Yes. Exactly. |
The mix-in helper classes like
Clearable<T>,Equatable<T>, andUnequatable<T>are intended for CRTP style inheritance, whereTis the derived class itself. A mistaken inheritance, such asclass Foo : public Clearable<Bar>, can compile successfully but lead to subtle bugs.This change enforces the correct CRTP usage at compile time. By making the constructors of these helper classes
privateand declaring the derived template classTas afriend, any incorrect inheritance will now result in a build failure. This approach correctly detects such a mistake, even ifFooandBarhappen to befriends of each other.Additionally,
Equatable<T>is updated to provide bothoperator==andoperator!=, removing its dependency onUnequatable<T>. This change allows us to apply theprivateconstructor enforcement toEquatable<T>as well.