Refactor numeric type handling and enhance conversion modules#18
Merged
FrozenLemonTee merged 11 commits intomainfrom Mar 26, 2026
Merged
Refactor numeric type handling and enhance conversion modules#18FrozenLemonTee merged 11 commits intomainfrom
FrozenLemonTee merged 11 commits intomainfrom
Conversation
… maintainability Signed-off-by: FrozenlemonTee <1115306170@qq.com>
…handling Signed-off-by: FrozenlemonTee <1115306170@qq.com>
…s to enhance type handling Signed-off-by: FrozenlemonTee <1115306170@qq.com>
…esentation bridges Signed-off-by: FrozenlemonTee <1115306170@qq.com>
…es and improve type safety Signed-off-by: FrozenlemonTee <1115306170@qq.com>
…nd floating point handling Signed-off-by: FrozenlemonTee <1115306170@qq.com>
Signed-off-by: FrozenlemonTee <1115306170@qq.com>
…type casting Signed-off-by: FrozenlemonTee <1115306170@qq.com>
…nterface Signed-off-by: FrozenlemonTee <1115306170@qq.com>
… module Signed-off-by: FrozenlemonTee <1115306170@qq.com>
…conversions Signed-off-by: FrozenlemonTee <1115306170@qq.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors numeric risk detection in the conversion subsystem to support underlying-type rep “bridging”, and adds new conversion modules to enable conversions between primitive<> wrappers and underlying types.
Changes:
- Refactors
conversion.underlyingnumeric risk handling to operate via underlying-type traits (including built-in proxy selection viacommon_rep_traits). - Adds new conversion modules for
primitive↔primitiveandprimitive↔underlyingcasting (conversion.primitive,conversion.mixing) and exports them fromconversion. - Extends test support types and adds new test coverage for numeric risk bridging and primitive/mixed conversions.
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/conversion/underlying.cppm |
Refactors numeric risk logic and adjusts cast API surface to work via underlying traits. |
src/conversion/mixing.cppm |
Adds overloads for casting between primitives and underlying types. |
src/conversion/primitive.cppm |
Adds primitive-to-primitive cast wrappers built on underlying conversions. |
src/conversion/conversion.cppm |
Re-exports the new conversion modules. |
tests/basic/support/conversion_box_types.hpp |
Adds bridged rep/box test types + underlying/common-rep trait specializations. |
tests/basic/conversion/traits/test_numeric_risk.cpp |
Adds tests for rep-bridge and built-in-proxy-bridge numeric risk detection. |
tests/basic/conversion/primitive/test_primitive_casts.cpp |
Adds tests for primitive-to-primitive casting behavior. |
tests/basic/conversion/primitive_underlying/test_mixed_casts.cpp |
Adds tests for underlying↔primitive mixed casting. |
tests/basic/*/test_*.cpp (various) |
Minor whitespace cleanup around includes. |
.gitignore |
Ignores cmake-build-* directories. |
Comments suppressed due to low confidence (1)
src/conversion/underlying.cppm:207
- The rep-level helpers (unchecked_rep_cast/checked_rep_cast/saturating_rep_cast/...) are constrained on numeric_underlying_type for both DestRep and SrcRep. These helpers are invoked with underlying::traits<...>::rep_type, which can be a custom numeric rep (i.e., not an underlying_type), so conversions involving custom reps will fail to compile (or be uncallable) even when a static_cast between reps exists. Consider relaxing these templates to accept arbitrary rep types (e.g., just require statically_castable), and keep numeric risk checking gated on std_numeric; then perform numeric_underlying_risk checks at the underlying-type layer when you want bridged risk detection.
template <numeric_underlying_type DestRep, numeric_underlying_type SrcRep>
requires statically_castable<DestRep, SrcRep>
constexpr auto unchecked_rep_cast(SrcRep value) noexcept
-> std::remove_cvref_t<DestRep> {
using dest_type = std::remove_cvref_t<DestRep>;
return static_cast<dest_type>(value);
}
template <numeric_underlying_type DestRep, numeric_underlying_type SrcRep>
requires statically_castable<DestRep, SrcRep>
constexpr auto checked_rep_cast(SrcRep value)
-> cast_result<std::remove_cvref_t<DestRep>> {
using dest_type = std::remove_cvref_t<DestRep>;
using src_type = std::remove_cvref_t<SrcRep>;
if constexpr (std_numeric<dest_type> && std_numeric<src_type>) {
if (auto const kind = numeric_risk<dest_type>(value); kind.has_value()) {
return std::unexpected(*kind);
}
}
return static_cast<dest_type>(value);
}
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.
No description provided.