narrow 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #ifndef GSL_NARROW_H
  17. #define GSL_NARROW_H
  18. #include "assert" // for GSL_SUPPRESS
  19. #include "util" // for narrow_cast
  20. #include <exception> // for std::exception
  21. namespace gsl
  22. {
  23. struct narrowing_error : public std::exception
  24. {
  25. const char* what() const noexcept override { return "narrowing_error"; }
  26. };
  27. // narrow() : a checked version of narrow_cast() that throws if the cast changed the value
  28. template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
  29. // clang-format off
  30. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  31. GSL_SUPPRESS(es.46) // NO-FORMAT: attribute // The warning suggests that a floating->unsigned conversion can occur
  32. // in the static_cast below, and that gsl::narrow should be used instead.
  33. // Suppress this warning, since gsl::narrow is defined in terms of
  34. // static_cast
  35. // clang-format on
  36. constexpr T narrow(U u)
  37. {
  38. constexpr const bool is_different_signedness =
  39. (std::is_signed<T>::value != std::is_signed<U>::value);
  40. GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow
  41. GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow
  42. GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
  43. const T t = narrow_cast<T>(u); // While this is technically undefined behavior in some cases (i.e., if the source value is of floating-point type
  44. // and cannot fit into the destination integral type), the resultant behavior is benign on the platforms
  45. // that we target (i.e., no hardware trap representations are hit).
  46. #if defined(__clang__) || defined(__GNUC__)
  47. #pragma GCC diagnostic push
  48. #pragma GCC diagnostic ignored "-Wfloat-equal"
  49. #endif
  50. // Note: NaN will always throw, since NaN != NaN
  51. if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{}))))
  52. {
  53. throw narrowing_error{};
  54. }
  55. #if defined(__clang__) || defined(__GNUC__)
  56. #pragma GCC diagnostic pop
  57. #endif
  58. return t;
  59. }
  60. template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
  61. // clang-format off
  62. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  63. // clang-format on
  64. constexpr T narrow(U u)
  65. {
  66. const T t = narrow_cast<T>(u);
  67. if (static_cast<U>(t) != u)
  68. {
  69. throw narrowing_error{};
  70. }
  71. return t;
  72. }
  73. } // namespace gsl
  74. #endif // GSL_NARROW_H