Skip to content

Commit 0f3439a

Browse files
[gf] Remove C4018 warnings from GfNumericCast
boolean types are considered (unsigned) integral types. Using boolean types with GfNumericCast would implicitly cast to a signed integer in GfIntegerCompareLess, causing Windows to issue C4018 warnings boolean types are now explicitly handled in GfNumericCast
1 parent d34c093 commit 0f3439a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pxr/base/gf/numericCast.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,27 @@ GfNumericCast(From from, GfNumericCastFailureType *failType = nullptr)
100100
};
101101
};
102102

103+
// bool -> int
104+
if constexpr (std::is_same_v<From, bool> && std::is_integral_v<To>) {
105+
// No need to range check bool to int
106+
return from ? static_cast<To>(1) : static_cast<To>(0);
107+
}
108+
// int -> bool
109+
else if constexpr (std::is_integral_v<From> && std::is_same_v<To, bool>) {
110+
// Range check int to bool
111+
if (GfIntegerCompareLess(from, 0)) {
112+
setFail(GfNumericCastNegOverflow);
113+
return {};
114+
}
115+
if (GfIntegerCompareLess(1, from)) {
116+
setFail(GfNumericCastPosOverflow);
117+
return {};
118+
}
119+
return from != 0;
120+
}
103121
// int -> int.
104-
if constexpr (std::is_integral_v<From> &&
105-
std::is_integral_v<To>) {
122+
else if constexpr (std::is_integral_v<From> &&
123+
std::is_integral_v<To>) {
106124
// Range check integer to integer.
107125
if (GfIntegerCompareLess(from, ToLimits::min())) {
108126
setFail(GfNumericCastNegOverflow);

0 commit comments

Comments
 (0)