Warning, /include/gsl/narrow is written in an unsupported language. File is not indexed.
0001 ///////////////////////////////////////////////////////////////////////////////
0002 //
0003 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
0004 //
0005 // This code is licensed under the MIT License (MIT).
0006 //
0007 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0008 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0009 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0010 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0011 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0012 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0013 // THE SOFTWARE.
0014 //
0015 ///////////////////////////////////////////////////////////////////////////////
0016
0017 #ifndef GSL_NARROW_H
0018 #define GSL_NARROW_H
0019 #include "./assert" // for GSL_SUPPRESS
0020 #include "./util" // for narrow_cast
0021 #include <exception> // for std::exception
0022 namespace gsl
0023 {
0024 struct narrowing_error : public std::exception
0025 {
0026 const char* what() const noexcept override { return "narrowing_error"; }
0027 };
0028
0029 // narrow() : a checked version of narrow_cast() that throws if the cast changed the value
0030 template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
0031 // clang-format off
0032 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
0033 GSL_SUPPRESS(es.46) // NO-FORMAT: attribute // The warning suggests that a floating->unsigned conversion can occur
0034 // in the static_cast below, and that gsl::narrow should be used instead.
0035 // Suppress this warning, since gsl::narrow is defined in terms of
0036 // static_cast
0037 // clang-format on
0038 constexpr T narrow(U u)
0039 {
0040 constexpr const bool is_different_signedness =
0041 (std::is_signed<T>::value != std::is_signed<U>::value);
0042
0043 GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow
0044 GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow
0045 GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
0046 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
0047 // and cannot fit into the destination integral type), the resultant behavior is benign on the platforms
0048 // that we target (i.e., no hardware trap representations are hit).
0049
0050 #if defined(__clang__) || defined(__GNUC__)
0051 #pragma GCC diagnostic push
0052 #pragma GCC diagnostic ignored "-Wfloat-equal"
0053 #endif
0054 // Note: NaN will always throw, since NaN != NaN
0055 if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{}))))
0056 {
0057 throw narrowing_error{};
0058 }
0059 #if defined(__clang__) || defined(__GNUC__)
0060 #pragma GCC diagnostic pop
0061 #endif
0062
0063 return t;
0064 }
0065
0066 template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
0067 // clang-format off
0068 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
0069 // clang-format on
0070 constexpr T narrow(U u)
0071 {
0072 const T t = narrow_cast<T>(u);
0073
0074 if (static_cast<U>(t) != u)
0075 {
0076 throw narrowing_error{};
0077 }
0078
0079 return t;
0080 }
0081 } // namespace gsl
0082 #endif // GSL_NARROW_H