From ef0ffefe525a6219ff245d19a832ce06f3fd3504 Mon Sep 17 00:00:00 2001 From: beinhaerter <34543625+beinhaerter@users.noreply.github.com> Date: Wed, 24 Feb 2021 23:39:13 +0100 Subject: [PATCH] is_comparable_to_nullptr for better static_assert (#975) * is_comparable_to_nullptr for better static_assert Trying `gsl::not_null p2{ 0 };` on VS2019 the current implementation would trigger >error C2446 : '!=' : no conversion from 'nullptr' to 'int' >message: A native nullptr can only be converted to bool or , using reinterpret_cast, to an integral type >message: see reference to class template instantiation 'gsl::not_null' being compiled >error C2955 : 'std::is_convertible' : use of class template requires template argument list >message: see declaration of 'std::is_convertible' >error C2039 : 'value' : is not a member of 'std::is_convertible<_From,_To>' >error C2065 : 'value' : undeclared identifier The new implementation gives much shorter and clearer message and does exactly as the `static_assert` intends to do: > error C2338: T cannot be compared to nullptr. > message : see reference to class template instantiation 'gsl::not_null' being compiled * Update include/gsl/pointers Co-authored-by: Casey Carter Co-authored-by: Werner Henze Co-authored-by: Casey Carter --- include/gsl/pointers | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/gsl/pointers b/include/gsl/pointers index 42e8c09..2f1b15f 100644 --- a/include/gsl/pointers +++ b/include/gsl/pointers @@ -32,6 +32,15 @@ namespace gsl { +namespace details +{ +template +struct is_comparable_to_nullptr : std::false_type {}; + +template +struct is_comparable_to_nullptr() != nullptr), bool>::value>> : std::true_type {}; +} // namespace details + // // GSL.owner: ownership pointers // @@ -68,8 +77,7 @@ template class not_null { public: - static_assert(std::is_convertible() != nullptr), bool>::value, - "T cannot be compared to nullptr."); + static_assert(details::is_comparable_to_nullptr::value, "T cannot be compared to nullptr."); template ::value>> constexpr not_null(U&& u) : ptr_(std::forward(u))