diff options
| author | Jordan Maples [MSFT] <49793787+JordanMaples@users.noreply.github.com> | 2020-08-13 16:35:04 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-13 16:35:04 -0700 |
| commit | 06c46195ee879a256a264edeaa5085ee3bdec17b (patch) | |
| tree | 0a654863dd1bb213c45e2724fbda69fbaa6506d2 | |
| parent | 83ce710d6c0719c4692ea6f79fe566db68ce497b (diff) | |
| parent | 74968d3ef87004ac04b6586bfe75e98c693a4c7b (diff) | |
| download | platform_external_Microsoft-GSL-06c46195ee879a256a264edeaa5085ee3bdec17b.tar.gz platform_external_Microsoft-GSL-06c46195ee879a256a264edeaa5085ee3bdec17b.tar.bz2 platform_external_Microsoft-GSL-06c46195ee879a256a264edeaa5085ee3bdec17b.zip | |
Merge pull request #910 from JordanMaples/exception_fix_new_file
exception free - Version 2
| -rw-r--r-- | include/gsl/gsl_narrow | 48 | ||||
| -rw-r--r-- | include/gsl/gsl_util | 25 | ||||
| -rw-r--r-- | tests/utils_tests.cpp | 4 |
3 files changed, 50 insertions, 27 deletions
diff --git a/include/gsl/gsl_narrow b/include/gsl/gsl_narrow new file mode 100644 index 0000000..7027b58 --- /dev/null +++ b/include/gsl/gsl_narrow @@ -0,0 +1,48 @@ +///////////////////////////////////////////////////////////////////////////////
+//
+// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
+//
+// This code is licensed under the MIT License (MIT).
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef GSL_NARROW_H
+#define GSL_NARROW_H
+#include <gsl/gsl_assert> // for Expects
+#include <gsl/gsl_util> // for narrow_cast
+namespace gsl
+{
+struct narrowing_error : public std::exception
+{
+};
+
+// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
+template <class T, class U>
+GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
+constexpr
+T narrow(U u) noexcept(false)
+{
+ constexpr const bool is_different_signedness = (std::is_signed<T>::value != std::is_signed<U>::value);
+
+ const T t = narrow_cast<T>(u);
+
+ if (static_cast<U>(t) != u
+ || (is_different_signedness
+ && ((t < T{}) != (u < U{}))))
+ {
+ throw narrowing_error{};
+ }
+
+ return t;
+}
+}
+#endif // GSL_NARROW_H
diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index 974655b..585b3f9 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -92,31 +92,6 @@ constexpr T narrow_cast(U&& u) noexcept return static_cast<T>(std::forward<U>(u)); } -struct narrowing_error : public std::exception -{ -}; - -// narrow() : a checked version of narrow_cast() that throws if the cast changed the value -template <class T, class U> -GSL_SUPPRESS(type.1) // NO-FORMAT: attribute -GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false) -constexpr -T narrow(U u) noexcept(false) -{ - constexpr const bool is_different_signedness = (std::is_signed<T>::value != std::is_signed<U>::value); - - const T t = narrow_cast<T>(u); - - if (static_cast<U>(t) != u - || (is_different_signedness - && ((t < T{}) != (u < U{})))) - { - throw narrowing_error{}; - } - - return t; -} - // // at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector // diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index ac83e2d..fae48f5 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -16,8 +16,8 @@ #include <gtest/gtest.h> -#include <gsl/gsl_util> // for narrow, finally, narrow_cast, narrowing_e... - +#include <gsl/gsl_util> // finally, narrow_cast +#include <gsl/gsl_narrow> // for narrow, narrowing_error #include <algorithm> // for move #include <functional> // for reference_wrapper, _Bind_helper<>::type #include <limits> // for numeric_limits |
