Skip to content

Commit

Permalink
not_null constructor is now explicit (#659)
Browse files Browse the repository at this point in the history
solves #395
  • Loading branch information
ericLemanissier authored and Anna Gringauze committed Apr 25, 2018
1 parent fbe867a commit 2fc94db
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/gsl/pointers
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public:
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");

template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr not_null(U&& u) : ptr_(std::forward<U>(u))
constexpr explicit not_null(U&& u) : ptr_(std::forward<U>(u))
{
Expects(ptr_ != nullptr);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/notnull_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ TEST_CASE("TestNotNullCasting")
MyBase base;
MyDerived derived;
Unrelated unrelated;
not_null<Unrelated*> u = &unrelated;
not_null<Unrelated*> u{&unrelated};
(void) u;
not_null<MyDerived*> p = &derived;
not_null<MyBase*> q = &base;
not_null<MyDerived*> p{&derived};
not_null<MyBase*> q(&base);
q = p; // allowed with heterogeneous copy ctor
CHECK(q == p);

Expand All @@ -192,18 +192,18 @@ TEST_CASE("TestNotNullCasting")
not_null<Unrelated*> r = p;
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
#endif
not_null<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get());
not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
}

TEST_CASE("TestNotNullAssignment")
{
int i = 12;
not_null<int*> p = &i;
not_null<int*> p(&i);
CHECK(helper(p));

int* q = nullptr;
CHECK_THROWS_AS(p = q, fail_fast);
CHECK_THROWS_AS(p = not_null<int*>(q), fail_fast);
}

TEST_CASE("TestNotNullRawPointerComparison")
Expand Down

0 comments on commit 2fc94db

Please sign in to comment.