Skip to content

Commit

Permalink
Merge pull request #873 from londey/narrow-throw
Browse files Browse the repository at this point in the history
Changed implementation of gsl::narrow to throw gsl::narrowing_error
  • Loading branch information
JordanMaples authored Apr 23, 2020
2 parents 9f6a9a5 + 3a5b83d commit 601b55e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 63 deletions.
8 changes: 6 additions & 2 deletions include/gsl/gsl_util
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ constexpr T narrow_cast(U&& u) noexcept
return static_cast<T>(std::forward<U>(u));
}

struct narrowing_error : public std::exception
{
};

namespace details
{
template <class T, class U>
Expand All @@ -115,9 +119,9 @@ constexpr
T narrow(U u) noexcept(false)
{
T t = narrow_cast<T>(u);
if (static_cast<U>(t) != u) gsl::details::terminate();
if (static_cast<U>(t) != u) throw narrowing_error{};
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
gsl::details::terminate();
throw narrowing_error{};
return t;
}

Expand Down
1 change: 0 additions & 1 deletion include/gsl/span
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include <gsl/gsl_assert> // for Expects
#include <gsl/gsl_byte> // for byte
#include <gsl/gsl_util> // for narrow_cast, narrow

#include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
Expand Down
1 change: 0 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,5 +264,4 @@ function(add_gsl_test_noexcept name)
set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests_noexcept")
endfunction()

add_gsl_test_noexcept(no_exception_throw_tests)
add_gsl_test_noexcept(no_exception_ensure_tests)
48 changes: 0 additions & 48 deletions tests/no_exception_throw_tests.cpp

This file was deleted.

15 changes: 4 additions & 11 deletions tests/utils_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ using namespace gsl;

namespace
{
static constexpr char deathstring[] = "Expected Death";
void f(int& i) { i += 1; }
static int j = 0;
void g() { j += 1; }
Expand Down Expand Up @@ -104,17 +103,12 @@ TEST(utils_tests, narrow_cast)

TEST(utils_tests, narrow)
{
std::set_terminate([] {
std::cerr << "Expected Death. narrow";
std::abort();
});

int n = 120;
const char c = narrow<char>(n);
EXPECT_TRUE(c == 120);

n = 300;
EXPECT_DEATH(narrow<char>(n), deathstring);
EXPECT_THROW(narrow<char>(n), narrowing_error);

const auto int32_max = std::numeric_limits<int32_t>::max();
const auto int32_min = std::numeric_limits<int32_t>::min();
Expand All @@ -123,14 +117,13 @@ TEST(utils_tests, narrow)
EXPECT_TRUE(narrow<uint32_t>(int32_t(1)) == 1);
EXPECT_TRUE(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));

EXPECT_DEATH(narrow<uint32_t>(int32_t(-1)), deathstring);
EXPECT_DEATH(narrow<uint32_t>(int32_min), deathstring);
EXPECT_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error);
EXPECT_THROW(narrow<uint32_t>(int32_min), narrowing_error);

n = -42;
EXPECT_DEATH(narrow<unsigned>(n), deathstring);
EXPECT_THROW(narrow<unsigned>(n), narrowing_error);

#if GSL_CONSTEXPR_NARROW
static_assert(narrow<char>(120) == 120, "Fix GSL_CONSTEXPR_NARROW");
#endif

}

0 comments on commit 601b55e

Please sign in to comment.