Skip to content

Commit 8db0cb2

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 43b8c71 commit 8db0cb2

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
@@ -86,9 +86,27 @@ GfNumericCast(From from, GfNumericCastFailureType *failType = nullptr)
8686
};
8787
};
8888

89+
// bool -> int
90+
if constexpr (std::is_same_v<From, bool> && std::is_integral_v<To>) {
91+
// No need to range check bool to int
92+
return from ? static_cast<To>(1) : static_cast<To>(0);
93+
}
94+
// int -> bool
95+
else if constexpr (std::is_integral_v<From> && std::is_same_v<To, bool>) {
96+
// Range check int to bool
97+
if (GfIntegerCompareLess(from, 0)) {
98+
setFail(GfNumericCastNegOverflow);
99+
return {};
100+
}
101+
if (GfIntegerCompareLess(1, from)) {
102+
setFail(GfNumericCastPosOverflow);
103+
return {};
104+
}
105+
return from != 0;
106+
}
89107
// int -> int.
90-
if constexpr (std::is_integral_v<From> &&
91-
std::is_integral_v<To>) {
108+
else if constexpr (std::is_integral_v<From> &&
109+
std::is_integral_v<To>) {
92110
// Range check integer to integer.
93111
if (GfIntegerCompareLess(from, ToLimits::min())) {
94112
setFail(GfNumericCastNegOverflow);

0 commit comments

Comments
 (0)