Fix Windows build (#17917)
authorAlexey Kozhevnikov <akozhevnikov@fb.com>
Wed, 13 Mar 2019 02:48:11 +0000 (19:48 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 13 Mar 2019 02:53:56 +0000 (19:53 -0700)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/17917

D14375995 introduced instantiation of the following templates with `bool` type (more specifically `To` is `int64_t`, `From` is `bool`):
```
template <typename To, typename From>
typename std::enable_if<std::is_integral<From>::value, bool>::type overflows(
    From f) {
  using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
  if (!limit::is_signed && std::numeric_limits<From>::is_signed) {
    // allow for negative numbers to wrap using two's complement arithmetic.
    // For example, with uint8, this allows for `a - b` to be treated as
    // `a + 255 * b`.
    return f > limit::max() ||
        (f < 0 && -static_cast<uint64_t>(f) > limit::max());
  } else {
    return f < limit::lowest() || f > limit::max();
  }
}

template <typename To, typename From>
typename std::enable_if<std::is_floating_point<From>::value, bool>::type
overflows(From f) {
  using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
  if (limit::has_infinity && std::isinf(static_cast<double>(f))) {
    return false;
  }
  if (!limit::has_quiet_NaN && (f != f)) {
    return true;
  }
  return f < limit::lowest() || f > limit::max();
}
```
MSVC gives C4804 warning and because "treat warnings as errors" is on it fails to build on Windows. Disabling such warning for those 2 templates.

Reviewed By: mingzhe09088

Differential Revision: D14421157

fbshipit-source-id: e72ba34406628c84da48518b32a46f851819bad1

c10/util/Half.h

index 37d12b1..3bb6f48 100644 (file)
@@ -419,10 +419,12 @@ struct Converter<
 
 // In some versions of MSVC, there will be a compiler error when building.
 // C4146: unary minus operator applied to unsigned type, result still unsigned
+// C4804: unsafe use of type 'bool' in operation
 // It can be addressed by disabling the following warning. 
 #ifdef _MSC_VER
 #pragma warning( push )
 #pragma warning( disable : 4146 )
+#pragma warning( disable : 4804 )
 #endif
 
 // skip isnan and isinf check for integral types
@@ -441,10 +443,6 @@ typename std::enable_if<std::is_integral<From>::value, bool>::type overflows(
   }
 }
 
-#ifdef _MSC_VER
-#pragma warning( pop )
-#endif
-
 template <typename To, typename From>
 typename std::enable_if<std::is_floating_point<From>::value, bool>::type
 overflows(From f) {
@@ -458,6 +456,10 @@ overflows(From f) {
   return f < limit::lowest() || f > limit::max();
 }
 
+#ifdef _MSC_VER
+#pragma warning( pop )
+#endif
+
 template <typename To, typename From>
 typename std::enable_if<is_complex_t<From>::value, bool>::type overflows(
     From f) {