Use operator<=> instead of std::less for UniqueID.#183600
Use operator<=> instead of std::less for UniqueID.#183600auto-submit[bot] merged 2 commits intoflutter:masterfrom
Conversation
Internally when this is compiled with C++20, the compiler could not find the implementation for `std::less<UniqueID, UniqueID>`. Overridding `operator<` instead as that is the best practice in modern C++.
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
There was a problem hiding this comment.
Code Review
This pull request updates the UniqueID struct to use operator< for comparison instead of a specialization of std::less. This is done by adding operator< to the UniqueID struct and removing the now-unnecessary std::less<impeller::UniqueID> specialization. This change aligns with modern C++ practices and resolves a compilation issue with C++20. I have one suggestion to improve the completeness of the UniqueID type by adding all relational operators.
| constexpr bool operator==(const UniqueID& other) const { | ||
| return id == other.id; | ||
| } | ||
|
|
||
| constexpr bool operator<(const UniqueID& other) const { | ||
| return id < other.id; | ||
| } |
There was a problem hiding this comment.
For completeness and to fully adhere to the Google C++ Style Guide, it's good practice to define all relational operators if you define any. Since operator== and operator< are now defined, consider adding !=, >, <=, and >= as well.
If C++20 is available, you could use the spaceship operator operator<=> to have the compiler generate all of these for you. For example:
#include <compare>
// ...
auto operator<=>(const UniqueID&) const = default;If not, they can be implemented in terms of operator== and operator< as shown in the suggestion.
constexpr bool operator==(const UniqueID& other) const {
return id == other.id;
}
constexpr bool operator!=(const UniqueID& other) const {
return !(*this == other);
}
constexpr bool operator<(const UniqueID& other) const {
return id < other.id;
}
constexpr bool operator>(const UniqueID& other) const {
return other < *this;
}
constexpr bool operator<=(const UniqueID& other) const {
return !(other < *this);
}
constexpr bool operator>=(const UniqueID& other) const {
return !(*this < other);
}References
- The Google C++ Style Guide, which this repository follows, suggests that if you define any comparison operators, you should define all of them that are logical for the type. (link)
There was a problem hiding this comment.
The engine is now using C++20. So you can replace the std::less and operator== implementations for UniqueID with the default <=> operator.
flutter/flutter@9e36adb...732e05d 2026-03-13 [email protected] Roll Dart SDK from 330b797abd09 to d5f6d3c17499 (1 revision) (flutter/flutter#183640) 2026-03-13 [email protected] Roll Skia from 9eb5598e1b2c to 029229d8be91 (3 revisions) (flutter/flutter#183638) 2026-03-13 [email protected] Roll Skia from 9be8fdf31ff4 to 9eb5598e1b2c (2 revisions) (flutter/flutter#183630) 2026-03-13 [email protected] Add awaits to flutter/test callsites (flutter/flutter#183487) 2026-03-13 [email protected] Add await to more flutter/flutter callsites (flutter/flutter#183413) 2026-03-13 [email protected] Roll Dart SDK from d1d84ab7ef0d to 330b797abd09 (2 revisions) (flutter/flutter#183624) 2026-03-13 [email protected] refactor: remove material import from sliver_resizing_header_test and sliver_prototype_item_extent_test (flutter/flutter#183562) 2026-03-13 [email protected] Fix reselection issue after the text is cleared (flutter/flutter#183545) 2026-03-13 [email protected] Roll Skia from 255bd243276b to 9be8fdf31ff4 (5 revisions) (flutter/flutter#183616) 2026-03-12 [email protected] ci: Remove `bringup` from orchestrator for windows_arm_host_engine on Linux (flutter/flutter#183574) 2026-03-12 [email protected] Use operator<=> instead of std::less for UniqueID. (flutter/flutter#183600) 2026-03-12 [email protected] Specified the repo the cp label will be removed from (flutter/flutter#183611) 2026-03-12 [email protected] [web] Fix Web SDK build on macOS (flutter/flutter#183549) 2026-03-12 [email protected] Roll Skia from 38761e1803d0 to 255bd243276b (3 revisions) (flutter/flutter#183603) 2026-03-12 [email protected] Roll Dart SDK from 2e1e7a09fce6 to d1d84ab7ef0d (1 revision) (flutter/flutter#183604) 2026-03-12 [email protected] Fix macOS relative plugin Xcode file path (flutter/flutter#183593) 2026-03-12 [email protected] Made cp labels get rejected on issues. (flutter/flutter#183595) 2026-03-12 [email protected] Roll Packages from ecace66 to 02f231f (4 revisions) (flutter/flutter#183594) 2026-03-12 [email protected] Roll Dart SDK from 59be21f25f2d to 2e1e7a09fce6 (1 revision) (flutter/flutter#183577) 2026-03-12 [email protected] Roll Skia from 46f41493ebf4 to 38761e1803d0 (6 revisions) (flutter/flutter#183590) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Internally when this is compiled with C++20, the compiler could not find the implementation for
std::less<UniqueID, UniqueID>. Overriddingoperator<=>instead as that is the best practice in modern C++.