You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
More partial merge of hsutter#106: Support signedness-only "narrowing" as contract precondition
Signed/unsigned conversions to a not-smaller type are handled as a precondition, and trying to cast from a source value that is in the half of the value space that isn't representable in the target type is flagged as a `Type` safety contract violation
// "No safe 'as' cast available - if this is narrowing and you're sure the conversion is safe, consider using `unsafe_narrow<T>()` to force the conversion"
715
-
// );
716
-
return nonesuch;
717
-
}
718
-
719
-
// For literals we can check for safe 'narrowing' at a compile time
708
+
// For literals we can check for safe 'narrowing' at a compile time (e.g., 1 as size_t)
720
709
template< typename C, auto x >
721
710
inlineconstexprbool is_castable_v =
722
-
std::is_arithmetic_v<C> &&
723
-
std::is_arithmetic_v<CPP2_TYPEOF(x)> &&
711
+
std::is_integral_v<C> &&
712
+
std::is_integral_v<CPP2_TYPEOF(x)> &&
724
713
!(static_cast<CPP2_TYPEOF(x)>(static_cast<C>(x)) != x ||
"No safe 'as' cast available - if this is narrowing and you're sure the conversion is safe, consider using `unsafe_narrow<T>()` to force the conversion"
743
-
);
744
-
}
722
+
723
+
template< typename C >
724
+
autoas(autoconst&) -> auto {
725
+
return nonesuch;
745
726
}
746
727
747
728
template< typename C, auto x >
@@ -758,6 +739,26 @@ inline constexpr auto as() -> auto
758
739
}
759
740
}
760
741
742
+
// Signed/unsigned conversions to a not-smaller type are handled as a precondition,
743
+
// and trying to cast from a value that is in the half of the value space that isn't
744
+
// representable in the target type C is flagged as a Type safety contract violation
0 commit comments