mirror of
https://github.com/microsoft/GSL.git
synced 2025-04-01 08:56:29 -04:00
move static_asserts to the original places, making the diff easier to read
This commit is contained in:
parent
f5ba439b11
commit
b438aa9470
@ -30,6 +30,13 @@
|
||||
#include "deathTestCommon.h"
|
||||
using namespace gsl;
|
||||
|
||||
#if __cplusplus >= 201703l
|
||||
using std::void_t;
|
||||
#else // __cplusplus >= 201703l
|
||||
template <class...>
|
||||
using void_t = void;
|
||||
#endif // __cplusplus < 201703l
|
||||
|
||||
struct MyBase
|
||||
{
|
||||
};
|
||||
@ -142,9 +149,38 @@ bool helper_const(not_null<const int*> p) { return *p == 12; }
|
||||
int* return_pointer() { return nullptr; }
|
||||
} // namespace
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool CtorCompilesFor_A = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
CtorCompilesFor_A<U, void_t<decltype(gsl::not_null<void*>{std::declval<U>()})>> = true;
|
||||
|
||||
template <typename U, int N, typename = void>
|
||||
static constexpr bool CtorCompilesFor_B = false;
|
||||
template <typename U, int N>
|
||||
static constexpr bool CtorCompilesFor_B<U, N, void_t<decltype(gsl::not_null<U>{N})>> = true;
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool DefaultCtorCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool DefaultCtorCompilesFor<U, void_t<decltype(gsl::not_null<U>{})>> = true;
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool CtorCompilesFor_C = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
CtorCompilesFor_C<U, void_t<decltype(gsl::not_null<U*>{std::declval<std::unique_ptr<U>>()})>> =
|
||||
true;
|
||||
|
||||
TEST(notnull_tests, TestNotNullConstructors)
|
||||
{
|
||||
{
|
||||
static_assert(CtorCompilesFor_A<void*>, "CtorCompilesFor_A<void*>");
|
||||
static_assert(!CtorCompilesFor_A<std::nullptr_t>, "!CtorCompilesFor_A<std::nullptr_t>");
|
||||
static_assert(!CtorCompilesFor_B<void*, 0>, "!CtorCompilesFor_B<void*, 0>");
|
||||
static_assert(!DefaultCtorCompilesFor<void*>, "!DefaultCtorCompilesFor<void*>");
|
||||
static_assert(!CtorCompilesFor_C<int>, "CtorCompilesFor_C<int>");
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
// Forbid non-nullptr assignable types
|
||||
not_null<std::vector<int>> f(std::vector<int>{1});
|
||||
@ -271,6 +307,27 @@ TEST(notnull_tests, TestNotNullostream)
|
||||
ostream_helper<std::string>("string");
|
||||
}
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool AssignmentCompilesFor = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool
|
||||
AssignmentCompilesFor<U, V,
|
||||
void_t<decltype(std::declval<gsl::not_null<U*>&>().operator=(
|
||||
std::declval<gsl::not_null<V*>&>()))>> = true;
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool SCastCompilesFor = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool
|
||||
SCastCompilesFor<U, V, void_t<decltype(static_cast<U*>(std::declval<gsl::not_null<V*>&>()))>> =
|
||||
true;
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool RCastCompilesFor = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool RCastCompilesFor<
|
||||
U, V, void_t<decltype(reinterpret_cast<U*>(std::declval<gsl::not_null<V*>&>()))>> = true;
|
||||
|
||||
TEST(notnull_tests, TestNotNullCasting)
|
||||
{
|
||||
MyBase base;
|
||||
@ -283,11 +340,30 @@ TEST(notnull_tests, TestNotNullCasting)
|
||||
q = p; // allowed with heterogeneous copy ctor
|
||||
EXPECT_TRUE(q == p);
|
||||
|
||||
static_assert(AssignmentCompilesFor<MyBase, MyDerived>,
|
||||
"AssignmentCompilesFor<MyBase, MyDerived>");
|
||||
static_assert(!AssignmentCompilesFor<MyBase, Unrelated>,
|
||||
"!AssignmentCompilesFor<MyBase, Unrelated>");
|
||||
static_assert(!AssignmentCompilesFor<Unrelated, MyDerived>,
|
||||
"!AssignmentCompilesFor<Unrelated, MyDerived>");
|
||||
static_assert(!AssignmentCompilesFor<MyDerived, MyBase>,
|
||||
"!AssignmentCompilesFor<MyDerived, MyBase>");
|
||||
|
||||
static_assert(SCastCompilesFor<MyDerived, MyDerived>, "SCastCompilesFor<MyDerived, MyDerived>");
|
||||
static_assert(SCastCompilesFor<MyBase, MyDerived>, "SCastCompilesFor<MyBase, MyDerived>");
|
||||
static_assert(!SCastCompilesFor<MyDerived, MyBase>, "!SCastCompilesFor<MyDerived, MyBase>");
|
||||
static_assert(!SCastCompilesFor<Unrelated, MyDerived>,
|
||||
"!SCastCompilesFor<Unrelated, MyDerived>");
|
||||
static_assert(!RCastCompilesFor<MyDerived, MyDerived>,
|
||||
"!SCastCompilesFor<MyDerived, MyDerived>");
|
||||
static_assert(!RCastCompilesFor<Unrelated, MyDerived>,
|
||||
"!SCastCompilesFor<Unrelated, MyDerived>");
|
||||
|
||||
not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
|
||||
EXPECT_TRUE(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
|
||||
|
||||
(void)static_cast<MyDerived*>(p);
|
||||
(void)static_cast<MyBase*>(p);
|
||||
(void) static_cast<MyDerived*>(p);
|
||||
(void) static_cast<MyBase*>(p);
|
||||
}
|
||||
|
||||
TEST(notnull_tests, TestNotNullAssignment)
|
||||
@ -429,6 +505,18 @@ TEST(notnull_tests, TestNotNullCustomPtrComparison)
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool TypeDeductionCtorCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
TypeDeductionCtorCompilesFor<U, void_t<decltype(not_null{std::declval<U>()})>> = true;
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool TypeDeductionHelperCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
TypeDeductionHelperCompilesFor<U, void_t<decltype(helper(not_null{std::declval<U>()}))>> = true;
|
||||
|
||||
TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
|
||||
{
|
||||
{
|
||||
@ -445,6 +533,9 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
|
||||
const int i = 42;
|
||||
|
||||
not_null x{&i};
|
||||
static_assert(TypeDeductionHelperCompilesFor<int*>, "TypeDeductionHelperCompilesFor<int*>");
|
||||
static_assert(!TypeDeductionHelperCompilesFor<const int*>,
|
||||
"!TypeDeductionHelperCompilesFor<const int*>");
|
||||
helper_const(not_null{&i});
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
@ -499,6 +590,17 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
|
||||
EXPECT_DEATH(helper(not_null{p}), expected);
|
||||
EXPECT_DEATH(helper_const(not_null{p}), expected);
|
||||
}
|
||||
|
||||
static_assert(TypeDeductionCtorCompilesFor<void*>, "TypeDeductionCtorCompilesFor<void*>");
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
// Fails on gcc, clang, xcode, VS clang with
|
||||
// "error : no type named 'type' in 'std::enable_if<false>'; 'enable_if' cannot be used to
|
||||
// disable this declaration"
|
||||
static_assert(!TypeDeductionCtorCompilesFor<std::nullptr_t>,
|
||||
"!TypeDeductionCtorCompilesFor<std::nullptr_t>");
|
||||
static_assert(!TypeDeductionHelperCompilesFor<std::nullptr_t>,
|
||||
"!TypeDeductionHelperCompilesFor<std::nullptr_t>");
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(notnull_tests, TestVariantEmplace)
|
||||
@ -513,6 +615,11 @@ TEST(notnull_tests, TestVariantEmplace)
|
||||
}
|
||||
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool HelperCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool HelperCompilesFor<U, void_t<decltype(helper(std::declval<U>()))>> = true;
|
||||
|
||||
TEST(notnull_tests, TestMakeNotNull)
|
||||
{
|
||||
{
|
||||
@ -529,6 +636,8 @@ TEST(notnull_tests, TestMakeNotNull)
|
||||
const int i = 42;
|
||||
|
||||
const auto x = make_not_null(&i);
|
||||
static_assert(HelperCompilesFor<gsl::not_null<int*>>,
|
||||
"HelperCompilesFor<gsl::not_null<int*>>");
|
||||
helper_const(make_not_null(&i));
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
@ -550,6 +659,8 @@ TEST(notnull_tests, TestMakeNotNull)
|
||||
const int* p = &i;
|
||||
|
||||
const auto x = make_not_null(p);
|
||||
static_assert(!HelperCompilesFor<gsl::not_null<const int*>>,
|
||||
"!HelperCompilesFor<gsl::not_null<const int*>>");
|
||||
helper_const(make_not_null(p));
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
@ -625,79 +736,3 @@ TEST(notnull_tests, TestStdHash)
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr));
|
||||
}
|
||||
}
|
||||
|
||||
#if __cplusplus >= 201703l
|
||||
using std::void_t;
|
||||
#else // __cplusplus >= 201703l
|
||||
template <class...>
|
||||
using void_t = void;
|
||||
#endif // __cplusplus < 201703l
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool CtorCompilesFor_A = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
CtorCompilesFor_A<U, void_t<decltype(gsl::not_null<void*>{std::declval<U>()})>> = true;
|
||||
static_assert(CtorCompilesFor_A<void*>, "CtorCompilesFor_A<void*>");
|
||||
static_assert(!CtorCompilesFor_A<std::nullptr_t>, "!CtorCompilesFor_A<std::nullptr_t>");
|
||||
|
||||
template <typename U, int N, typename = void>
|
||||
static constexpr bool CtorCompilesFor_B = false;
|
||||
template <typename U, int N>
|
||||
static constexpr bool CtorCompilesFor_B<U, N, void_t<decltype(gsl::not_null<U>{N})>> = true;
|
||||
static_assert(!CtorCompilesFor_B<void*, 0>, "!CtorCompilesFor_B<void*, 0>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool CtorCompilesFor_C = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
CtorCompilesFor_C<U, void_t<decltype(gsl::not_null<U*>{std::declval<std::unique_ptr<U>>()})>> =
|
||||
true;
|
||||
static_assert(!CtorCompilesFor_C<int>, "CtorCompilesFor_C<int>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool DefaultCtorCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool DefaultCtorCompilesFor<U, void_t<decltype(gsl::not_null<U>{})>> = true;
|
||||
static_assert(!DefaultCtorCompilesFor<void*>, "!DefaultCtorCompilesFor<void*>");
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool AssignmentCompilesFor = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool
|
||||
AssignmentCompilesFor<U, V,
|
||||
void_t<decltype(std::declval<gsl::not_null<U*>&>().operator=(
|
||||
std::declval<gsl::not_null<V*>&>()))>> = true;
|
||||
static_assert(AssignmentCompilesFor<MyBase, MyDerived>, "AssignmentCompilesFor<MyBase, MyDerived>");
|
||||
static_assert(!AssignmentCompilesFor<MyBase, Unrelated>,
|
||||
"!AssignmentCompilesFor<MyBase, Unrelated>");
|
||||
static_assert(!AssignmentCompilesFor<Unrelated, MyDerived>,
|
||||
"!AssignmentCompilesFor<Unrelated, MyDerived>");
|
||||
static_assert(!AssignmentCompilesFor<MyDerived, MyBase>,
|
||||
"!AssignmentCompilesFor<MyDerived, MyBase>");
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool CastCompilesFor_A = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool CastCompilesFor_A<
|
||||
U, V, void_t<decltype(static_cast<U*>(std::declval<gsl::not_null<V*>&>()))>> = true;
|
||||
static_assert(CastCompilesFor_A<MyDerived, MyDerived>, "CastCompilesFor_A<MyDerived, MyDerived>");
|
||||
static_assert(CastCompilesFor_A<MyBase, MyDerived>, "CastCompilesFor_A<MyBase, MyDerived>");
|
||||
static_assert(!CastCompilesFor_A<MyDerived, MyBase>, "!CastCompilesFor_A<MyDerived, MyBase>");
|
||||
static_assert(!CastCompilesFor_A<Unrelated, MyDerived>, "!CastCompilesFor_A<Unrelated, MyDerived>");
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool CastCompilesFor_B = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool CastCompilesFor_B<
|
||||
U, V, void_t<decltype(reinterpret_cast<U*>(std::declval<gsl::not_null<V*>&>()))>> = true;
|
||||
static_assert(!CastCompilesFor_B<MyDerived, MyDerived>, "!CastCompilesFor_A<MyDerived, MyDerived>");
|
||||
static_assert(!CastCompilesFor_B<Unrelated, MyDerived>, "!CastCompilesFor_A<Unrelated, MyDerived>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool HelperCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool HelperCompilesFor<U, void_t<decltype(helper(std::declval<U>()))>> = true;
|
||||
static_assert(HelperCompilesFor<gsl::not_null<int*>>, "HelperCompilesFor<gsl::not_null<int*>>");
|
||||
static_assert(!HelperCompilesFor<gsl::not_null<const int*>>,
|
||||
"!HelperCompilesFor<gsl::not_null<const int*>>");
|
||||
|
@ -6,6 +6,13 @@
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#if __cplusplus >= 201703l
|
||||
using std::void_t;
|
||||
#else // __cplusplus >= 201703l
|
||||
template <class...>
|
||||
using void_t = void;
|
||||
#endif // __cplusplus < 201703l
|
||||
|
||||
namespace
|
||||
{
|
||||
// Custom pointer type that can be used for gsl::not_null, but for which these cannot be swapped.
|
||||
@ -22,6 +29,13 @@ struct NotMoveAssignableCustomPtr
|
||||
int dummy{}; // Without this clang warns, that NotMoveAssignableCustomPtr() is unneeded
|
||||
};
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool SwapCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
SwapCompilesFor<U, void_t<decltype(gsl::swap<U>(std::declval<gsl::not_null<U>&>(),
|
||||
std::declval<gsl::not_null<U>&>()))>> = true;
|
||||
|
||||
TEST(pointers_test, swap)
|
||||
{
|
||||
// taken from gh-1129:
|
||||
@ -69,22 +83,9 @@ TEST(pointers_test, swap)
|
||||
EXPECT_TRUE(*a == 1);
|
||||
EXPECT_TRUE(*b == 0);
|
||||
}
|
||||
|
||||
static_assert(!SwapCompilesFor<NotMoveAssignableCustomPtr>,
|
||||
"!SwapCompilesFor<NotMoveAssignableCustomPtr>");
|
||||
}
|
||||
|
||||
#if __cplusplus >= 201703l
|
||||
using std::void_t;
|
||||
#else // __cplusplus >= 201703l
|
||||
template <class...>
|
||||
using void_t = void;
|
||||
#endif // __cplusplus < 201703l
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool SwapCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
SwapCompilesFor<U, void_t<decltype(gsl::swap<U>(std::declval<gsl::not_null<U>&>(),
|
||||
std::declval<gsl::not_null<U>&>()))>> = true;
|
||||
static_assert(!SwapCompilesFor<NotMoveAssignableCustomPtr>,
|
||||
"!SwapCompilesFor<NotMoveAssignableCustomPtr>");
|
||||
|
||||
} // namespace
|
||||
|
@ -48,6 +48,13 @@
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
#if __cplusplus >= 201703l
|
||||
using std::void_t;
|
||||
#else // __cplusplus >= 201703l
|
||||
template <class...>
|
||||
using void_t = void;
|
||||
#endif // __cplusplus < 201703l
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
@ -215,13 +222,11 @@ TEST(span_test, from_pointer_length_constructor)
|
||||
|
||||
TEST(span_test, from_pointer_pointer_construction)
|
||||
{
|
||||
#if !(defined _MSVC_STL_VERSION) || defined(NDEBUG)
|
||||
const auto terminateHandler = std::set_terminate([] {
|
||||
std::cerr << "Expected Death. from_pointer_pointer_construction";
|
||||
std::abort();
|
||||
});
|
||||
const auto expected = GetExpectedDeathString(terminateHandler);
|
||||
#endif
|
||||
// const auto terminateHandler = std::set_terminate([] {
|
||||
// std::cerr << "Expected Death. from_pointer_pointer_construction";
|
||||
// std::abort();
|
||||
// });
|
||||
// const auto expected = GetExpectedDeathString(terminateHandler);
|
||||
|
||||
int arr[4] = {1, 2, 3, 4};
|
||||
|
||||
@ -252,20 +257,18 @@ TEST(span_test, from_pointer_pointer_construction)
|
||||
EXPECT_TRUE(s.data() == &arr[0]);
|
||||
}
|
||||
|
||||
#if !(defined _MSVC_STL_VERSION) || defined(NDEBUG)
|
||||
// this will fail the std::distance() precondition, which asserts on MSVC debug builds
|
||||
{
|
||||
auto workaround_macro = [&]() { span<int> s{&arr[1], &arr[0]}; };
|
||||
EXPECT_DEATH(workaround_macro(), expected);
|
||||
}
|
||||
//{
|
||||
// auto workaround_macro = [&]() { span<int> s{&arr[1], &arr[0]}; };
|
||||
// EXPECT_DEATH(workaround_macro(), expected);
|
||||
//}
|
||||
|
||||
// this will fail the std::distance() precondition, which asserts on MSVC debug builds
|
||||
{
|
||||
int* p = nullptr;
|
||||
auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
|
||||
EXPECT_DEATH(workaround_macro(), expected);
|
||||
}
|
||||
#endif
|
||||
//{
|
||||
// int* p = nullptr;
|
||||
// auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
|
||||
// EXPECT_DEATH(workaround_macro(), expected);
|
||||
//}
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
@ -281,20 +284,27 @@ TEST(span_test, from_pointer_pointer_construction)
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
}
|
||||
|
||||
#if !(defined _MSVC_STL_VERSION) || defined(NDEBUG)
|
||||
// this will fail the std::distance() precondition, which asserts on MSVC debug builds
|
||||
{
|
||||
int* p = nullptr;
|
||||
auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
|
||||
EXPECT_DEATH(workaround_macro(), expected);
|
||||
}
|
||||
#endif
|
||||
//{
|
||||
// int* p = nullptr;
|
||||
// auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
|
||||
// EXPECT_DEATH(workaround_macro(), expected);
|
||||
//}
|
||||
}
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool CtorCompilesFor = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool CtorCompilesFor<U, V, void_t<decltype(U{std::declval<V>()})>> = true;
|
||||
|
||||
TEST(span_test, from_array_constructor)
|
||||
{
|
||||
int arr[5] = {1, 2, 3, 4, 5};
|
||||
|
||||
static_assert(!CtorCompilesFor<span<int, 6>, int[5]>, "!CtorCompilesFor<span<int, 6>, int[5]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 0>, int[5]>, "!CtorCompilesFor<span<int, 0>, int[5]>");
|
||||
static_assert(!CtorCompilesFor<span<int>, int[2][3]>, "!CtorCompilesFor<span<int>, int[2][3]>");
|
||||
|
||||
{
|
||||
const span<int> s{arr};
|
||||
EXPECT_TRUE(s.size() == 5);
|
||||
@ -309,6 +319,11 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
int arr2d[2][3] = {1, 2, 3, 4, 5, 6};
|
||||
|
||||
static_assert(!CtorCompilesFor<span<int, 0>, int[2][3]>,
|
||||
"!CtorCompilesFor<span<int, 0>, int[2][3]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 6>, int[2][3]>,
|
||||
"!CtorCompilesFor<span<int, 6>, int[2][3]>");
|
||||
|
||||
{
|
||||
const span<int[3]> s{std::addressof(arr2d[0]), 1};
|
||||
EXPECT_TRUE(s.size() == 1);
|
||||
@ -317,6 +332,15 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
int arr3d[2][3][2] = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}};
|
||||
|
||||
static_assert(!CtorCompilesFor<span<int>, int[2][3][2]>,
|
||||
"!CtorCompilesFor<span<int>, int[2][3][2]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 0>, int[2][3][2]>,
|
||||
"!CtorCompilesFor<span<int, 0>, int[2][3][2]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 11>, int[2][3][2]>,
|
||||
"!CtorCompilesFor<span<int, 11>, int[2][3][2]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 12>, int[2][3][2]>,
|
||||
"!CtorCompilesFor<span<int, 12>, int[2][3][2]>");
|
||||
|
||||
{
|
||||
const span<int[3][2]> s{std::addressof(arr3d[0]), 1};
|
||||
EXPECT_TRUE(s.size() == 1);
|
||||
@ -344,6 +368,13 @@ TEST(span_test, from_dynamic_array_constructor)
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool ConversionCompilesFor = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool
|
||||
ConversionCompilesFor<U, V, void_t<decltype(std::declval<void (*)(U)>()(std::declval<V>()))>> =
|
||||
true;
|
||||
|
||||
TEST(span_test, from_std_array_constructor)
|
||||
{
|
||||
std::array<int, 4> arr = {1, 2, 3, 4};
|
||||
@ -383,10 +414,31 @@ TEST(span_test, from_std_array_constructor)
|
||||
EXPECT_TRUE(ao_arr.data() == fs.data());
|
||||
}
|
||||
|
||||
static_assert(!CtorCompilesFor<span<int, 2>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 2>, std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>");
|
||||
|
||||
static_assert(!CtorCompilesFor<span<int, 0>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 0>, std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>");
|
||||
|
||||
static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>");
|
||||
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
|
||||
static_assert(!ConversionCompilesFor<span<int>, std::array<int, 4>>,
|
||||
"!ConversionCompilesFor<span<int>, std::array<int, 4>>");
|
||||
#endif
|
||||
|
||||
{
|
||||
auto get_an_array = []() -> std::array<int, 4> { return {1, 2, 3, 4}; };
|
||||
auto take_a_span = [](span<const int>) {};
|
||||
// try to take a temporary std::array
|
||||
static_assert(ConversionCompilesFor<span<const int>, std::array<int, 4>>,
|
||||
"ConversionCompilesFor<span<const int>, std::array<int, 4>>");
|
||||
take_a_span(get_an_array());
|
||||
}
|
||||
}
|
||||
@ -415,6 +467,13 @@ TEST(span_test, from_const_std_array_constructor)
|
||||
EXPECT_TRUE(s.data() == ao_arr.data());
|
||||
}
|
||||
|
||||
static_assert(!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>");
|
||||
|
||||
{
|
||||
auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; };
|
||||
auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
|
||||
@ -438,6 +497,15 @@ TEST(span_test, from_std_array_const_constructor)
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
|
||||
static_assert(!CtorCompilesFor<span<const int, 2>, const std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 2>, const std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 0>, const std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 0>, const std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 5>, const std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 5>, const std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<int, 5>, const std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 5>, const std::array<int, 4>&>");
|
||||
}
|
||||
|
||||
TEST(span_test, from_container_constructor)
|
||||
@ -459,11 +527,8 @@ TEST(span_test, from_container_constructor)
|
||||
const std::string cstr = "hello";
|
||||
|
||||
{
|
||||
#if __cplusplus >= 201703L
|
||||
span<char> s{str};
|
||||
EXPECT_TRUE(s.size() == str.size());
|
||||
EXPECT_TRUE(s.data() == str.data());
|
||||
#endif
|
||||
static_assert(CtorCompilesFor<span<char>, std::string&> == (__cplusplus >= 201703L),
|
||||
"CtorCompilesFor<span<char>, std::string&> == (__cplusplus >= 201703L)");
|
||||
|
||||
span<const char> cs{str};
|
||||
EXPECT_TRUE(cs.size() == str.size());
|
||||
@ -471,28 +536,48 @@ TEST(span_test, from_container_constructor)
|
||||
}
|
||||
|
||||
{
|
||||
static_assert(!CtorCompilesFor<span<char>, const std::string&>,
|
||||
"!CtorCompilesFor<span<char>, const std::string&>");
|
||||
|
||||
span<const char> cs{cstr};
|
||||
EXPECT_TRUE(cs.size() == cstr.size());
|
||||
EXPECT_TRUE(cs.data() == cstr.data());
|
||||
}
|
||||
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
|
||||
static_assert(!ConversionCompilesFor<span<int>, std::vector<int>>,
|
||||
"!ConversionCompilesFor<span<int>, std::vector<int>>");
|
||||
#endif // !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
|
||||
{
|
||||
auto get_temp_vector = []() -> std::vector<int> { return {}; };
|
||||
auto use_span = [](span<const int> s) { static_cast<void>(s); };
|
||||
use_span(get_temp_vector());
|
||||
}
|
||||
|
||||
static_assert(!ConversionCompilesFor<span<char>, std::string>,
|
||||
"!ConversionCompilesFor<span<char>, std::string>");
|
||||
|
||||
{
|
||||
auto get_temp_string = []() -> std::string { return {}; };
|
||||
auto use_span = [](span<const char> s) { static_cast<void>(s); };
|
||||
use_span(get_temp_string());
|
||||
}
|
||||
|
||||
static_assert(!ConversionCompilesFor<span<const char>, const std::vector<int>>,
|
||||
"!ConversionCompilesFor<span<const char>, const std::vector<int>>");
|
||||
static_assert(!ConversionCompilesFor<span<char>, const std::string>,
|
||||
"!ConversionCompilesFor<span<char>, const std::string>");
|
||||
|
||||
{
|
||||
auto get_temp_string = []() -> const std::string { return {}; };
|
||||
auto use_span = [](span<const char> s) { static_cast<void>(s); };
|
||||
use_span(get_temp_string());
|
||||
}
|
||||
|
||||
static_assert(!CtorCompilesFor<span<int>, std::map<int, int>&>,
|
||||
"!CtorCompilesFor<span<int>, std::map<int, int>&>");
|
||||
}
|
||||
|
||||
TEST(span_test, from_convertible_span_constructor)
|
||||
@ -543,6 +628,21 @@ TEST(span_test, from_convertible_span_constructor)
|
||||
using T = span<const DerivedClass, 2>;
|
||||
EXPECT_DEATH(T{avd}, expected);
|
||||
}
|
||||
|
||||
static_assert(!ConversionCompilesFor<span<const DerivedClass, 2>, span<DerivedClass>&>,
|
||||
"!ConversionCompilesFor<span<const DerivedClass, 2>, span<DerivedClass>&>");
|
||||
static_assert(!ConversionCompilesFor<span<const DerivedClass, 1>, span<DerivedClass, 2>&>,
|
||||
"!ConversionCompilesFor<span<const DerivedClass, 1>, span<DerivedClass, 2>&>");
|
||||
static_assert(!ConversionCompilesFor<span<const DerivedClass, 3>, span<DerivedClass, 2>&>,
|
||||
"!ConversionCompilesFor<span<const DerivedClass, 3>, span<DerivedClass, 2>&>");
|
||||
static_assert(!ConversionCompilesFor<span<BaseClass, 3>, span<DerivedClass>&>,
|
||||
"!ConversionCompilesFor<span<BaseClass, 3>, span<DerivedClass>&>");
|
||||
static_assert(!ConversionCompilesFor<span<unsigned int>, span<int>&>,
|
||||
"!ConversionCompilesFor<span<unsigned int>, span<int>&>");
|
||||
static_assert(!ConversionCompilesFor<span<const unsigned int>, span<int>&>,
|
||||
"!ConversionCompilesFor<span<const unsigned int>, span<int>&>");
|
||||
static_assert(!ConversionCompilesFor<span<short>, span<int>&>,
|
||||
"!ConversionCompilesFor<span<short>, span<int>&>");
|
||||
}
|
||||
|
||||
TEST(span_test, copy_move_and_assignment)
|
||||
@ -911,9 +1011,21 @@ TEST(span_test, rbegin_rend)
|
||||
}
|
||||
}
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool AsWritableBytesCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
AsWritableBytesCompilesFor<U, void_t<decltype(as_writable_bytes(std::declval<U>()))>> = true;
|
||||
|
||||
TEST(span_test, as_bytes)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
|
||||
static_assert(AsWritableBytesCompilesFor<span<int>>, "AsWriteableBytesCompilesFor<span<int>>");
|
||||
// you should not be able to get writeable bytes for const objects
|
||||
static_assert(!AsWritableBytesCompilesFor<span<const int>>,
|
||||
"!AsWriteableBytesCompilesFor<span<const int>>");
|
||||
|
||||
{
|
||||
const span<const int> s = a;
|
||||
EXPECT_TRUE(s.size() == 4);
|
||||
@ -983,6 +1095,18 @@ TEST(span_test, fixed_size_conversions)
|
||||
static_cast<void>(s);
|
||||
}
|
||||
|
||||
// initialization or assignment to static span that REDUCES size is NOT ok
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, int[4]>,
|
||||
"!ConversionCompilesFor<span<int, 2>, int[4]>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, span<int, 4>>,
|
||||
"!ConversionCompilesFor<span<int, 2>, span<int, 4>>");
|
||||
|
||||
// even when done dynamically
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, span<int>>,
|
||||
"!ConversionCompilesFor<span<int, 2>, span<int>>");
|
||||
|
||||
// but doing so explicitly is ok
|
||||
|
||||
// you can convert statically
|
||||
{
|
||||
const span<int, 2> s2{&arr[0], 2};
|
||||
@ -993,9 +1117,24 @@ TEST(span_test, fixed_size_conversions)
|
||||
static_cast<void>(s1);
|
||||
}
|
||||
|
||||
// this is not a legal operation in std::span, so we are no longer supporting it
|
||||
// conversion from span<int, 4> to span<int, dynamic_extent> via call to `first`
|
||||
// then convert from span<int, dynamic_extent> to span<int, 1>
|
||||
// The dynamic to fixed extents are not supported in the standard
|
||||
// to make this work, span<int, 1> would need to be span<int>.
|
||||
static_assert(!ConversionCompilesFor<span<int, 1>, span<int>>,
|
||||
"!ConversionCompilesFor<span<int, 1>, span<int>>");
|
||||
|
||||
// initialization or assignment to static span that requires size INCREASE is not ok.
|
||||
int arr2[2] = {1, 2};
|
||||
|
||||
static_assert(!ConversionCompilesFor<span<int, 4>, int[2]>,
|
||||
"!ConversionCompilesFor<span<int, 4>, int[2]>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, int[2]>,
|
||||
"!ConversionCompilesFor<span<int, 2>, int[2]>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 4>, span<int, 2>>,
|
||||
"!ConversionCompilesFor<span<int, 4>, span<int, 2>>");
|
||||
|
||||
{
|
||||
auto f = [&]() {
|
||||
const span<int, 4> _s4{arr2, 2};
|
||||
@ -1003,6 +1142,12 @@ TEST(span_test, fixed_size_conversions)
|
||||
};
|
||||
EXPECT_DEATH(f(), expected);
|
||||
}
|
||||
|
||||
// This no longer compiles. There is no suitable conversion from dynamic span to a fixed size
|
||||
// span.
|
||||
// this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
|
||||
static_assert(!ConversionCompilesFor<span<int, 4>, span<int>>,
|
||||
"!ConversionCompilesFor<span<int, 4>, span<int>>");
|
||||
}
|
||||
|
||||
TEST(span_test, interop_with_std_regex)
|
||||
@ -1104,175 +1249,3 @@ TEST(span_test, msvc_compile_error_PR1100)
|
||||
for (const auto& e : sp) { (void) e; }
|
||||
}
|
||||
#endif // defined(__cpp_lib_span) && defined(__cpp_lib_ranges)
|
||||
|
||||
#if __cplusplus >= 201703l
|
||||
using std::void_t;
|
||||
#else // __cplusplus >= 201703l
|
||||
template <class...>
|
||||
using void_t = void;
|
||||
#endif // __cplusplus < 201703l
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool CtorCompilesFor = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool CtorCompilesFor<U, V, void_t<decltype(U{std::declval<V>()})>> = true;
|
||||
static_assert(CtorCompilesFor<span<int>, std::array<int, 12>&>,
|
||||
"CtorCompilesFor<span<int>, std::array<int, 12>&>");
|
||||
static_assert(CtorCompilesFor<span<int, 4>, std::array<int, 4>&>,
|
||||
"CtorCompilesFor<span<int, 4>, std::array<int, 4>&>");
|
||||
static_assert(CtorCompilesFor<span<int*>, std::array<int*, 12>&>,
|
||||
"CtorCompilesFor<span<int*>, std::array<int*, 12>&>");
|
||||
static_assert(!CtorCompilesFor<span<int*, 4>, std::array<int*, 12>&>,
|
||||
"!CtorCompilesFor<span<int*, 4>, std::array<int*, 12>&>");
|
||||
static_assert(CtorCompilesFor<span<int, 6>, std::array<int, 6>&>,
|
||||
"CtorCompilesFor<span<int, 6>, std::array<int, 6>&>");
|
||||
static_assert(!CtorCompilesFor<span<int, 6>, std::array<int, 5>&>,
|
||||
"!CtorCompilesFor<span<int, 6>, std::array<int, 5>&>");
|
||||
static_assert(!CtorCompilesFor<span<int, 0>, std::array<int, 5>&>,
|
||||
"!CtorCompilesFor<span<int, 0>, std::array<int, 5>&>");
|
||||
static_assert(!CtorCompilesFor<span<int>, int[2][3]>, "!CtorCompilesFor<span<int>, int[2][3]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 0>, int[2][3]>,
|
||||
"!CtorCompilesFor<span<int, 0>, int[2][3]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 6>, int[2][3]>,
|
||||
"!CtorCompilesFor<span<int, 6>, int[2][3]>");
|
||||
static_assert(!CtorCompilesFor<span<int>, int[2][3][2]>,
|
||||
"!CtorCompilesFor<span<int>, int[2][3][2]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 0>, int[2][3][2]>,
|
||||
"!CtorCompilesFor<span<int, 0>, int[2][3][2]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 11>, int[2][3][2]>,
|
||||
"!CtorCompilesFor<span<int, 11>, int[2][3][2]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 12>, int[2][3][2]>,
|
||||
"!CtorCompilesFor<span<int, 12>, int[2][3][2]>");
|
||||
static_assert(!CtorCompilesFor<span<int, 2>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 2>, std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<int, 0>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 0>, std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>");
|
||||
static_assert(CtorCompilesFor<span<int, 4>, std::array<int, 4>&>,
|
||||
"CtorCompilesFor<span<int, 4>, std::array<int, 4>&>");
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
|
||||
static_assert(!CtorCompilesFor<span<int, 4>, std::array<int, 4>&&>,
|
||||
"!CtorCompilesFor<span<int, 4>, std::array<int, 4>&&>");
|
||||
#endif // !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
static_assert(!CtorCompilesFor<span<const int, 2>, const std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 2>, const std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 0>, const std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 0>, const std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 5>, const std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 5>, const std::array<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 2>, std::array<const int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 2>, std::array<const int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 0>, std::array<const int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 0>, std::array<const int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<const int, 5>, std::array<const int, 4>&>,
|
||||
"!CtorCompilesFor<span<const int, 5>, std::array<const int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<int, 4>, std::array<const int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 4>, std::array<const int, 4>&>");
|
||||
static_assert(CtorCompilesFor<span<const char>, const std::string&>,
|
||||
"CtorCompilesFor<span<const char>, const std::string&>");
|
||||
static_assert(!CtorCompilesFor<span<char>, const std::string&>,
|
||||
"!CtorCompilesFor<span<char>, const std::string&>");
|
||||
|
||||
static_assert(CtorCompilesFor<span<int>, std::vector<int>&>,
|
||||
"CtorCompilesFor<span<int>, std::vector<int>&>");
|
||||
static_assert(CtorCompilesFor<span<const int>, std::vector<int>&&>,
|
||||
"CtorCompilesFor<span<const int>, std::vector<int>&&>");
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
|
||||
static_assert(!CtorCompilesFor<span<int>, std::vector<int>&&>,
|
||||
"!CtorCompilesFor<span<int>, std::vector<int>&&>");
|
||||
#endif // !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
static_assert(!CtorCompilesFor<span<int>, const std::vector<int>&&>,
|
||||
"!CtorCompilesFor<span<int>, const std::vector<int>&&>");
|
||||
|
||||
#if __cplusplus < 201703L
|
||||
static_assert(!CtorCompilesFor<span<char>, std::string&>,
|
||||
"!CtorCompilesFor<span<char>, std::string&>");
|
||||
#endif
|
||||
static_assert(CtorCompilesFor<span<const char>, std::string&&>,
|
||||
"CtorCompilesFor<span<const char>, std::string&&>");
|
||||
static_assert(!CtorCompilesFor<span<char>, std::string&&>,
|
||||
"!CtorCompilesFor<span<char>, std::string&&>");
|
||||
static_assert(!CtorCompilesFor<span<char>, const std::string&&>,
|
||||
"!CtorCompilesFor<span<char>, const std::string&&>");
|
||||
|
||||
static_assert(!CtorCompilesFor<span<int>, std::map<int, int>&>,
|
||||
"!CtorCompilesFor<span<int>, std::map<int, int>&>");
|
||||
|
||||
static_assert(CtorCompilesFor<span<int>, span<int, 4>&>,
|
||||
"CtorCompilesFor<span<int>, span<int, 4>&>");
|
||||
static_assert(CtorCompilesFor<span<int, 4>, span<int, 4>&>,
|
||||
"CtorCompilesFor<span<int, 4>, span<int, 4>&>");
|
||||
static_assert(!CtorCompilesFor<span<int, 2>, span<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 2>, span<int, 4>&>");
|
||||
|
||||
template <typename U, typename V, typename = void>
|
||||
static constexpr bool ConversionCompilesFor = false;
|
||||
template <typename U, typename V>
|
||||
static constexpr bool
|
||||
ConversionCompilesFor<U, V, void_t<decltype(std::declval<void (*)(U)>()(std::declval<V>()))>> =
|
||||
true;
|
||||
static_assert(ConversionCompilesFor<span<int, 2>, span<int, 2>>,
|
||||
"ConversionCompilesFor<span<int, 2>, span<int, 2>>");
|
||||
static_assert(ConversionCompilesFor<span<const int, 2>, span<int, 2>>,
|
||||
"ConversionCompilesFor<span<const int, 2>, span<int, 2>>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, std::array<int, 4>>,
|
||||
"!ConversionCompilesFor<span<int, 2>, std::array<int, 4>>");
|
||||
static_assert(!ConversionCompilesFor<span<const int, 2>, std::array<int, 4>>,
|
||||
"!ConversionCompilesFor<span<const int, 2>, std::array<int, 4>>");
|
||||
static_assert(ConversionCompilesFor<span<const int>, std::vector<int>>,
|
||||
"ConversionCompilesFor<span<const int>, std::vector<int>>");
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
|
||||
static_assert(!ConversionCompilesFor<span<int>, std::vector<int>>,
|
||||
"!ConversionCompilesFor<span<int>, std::vector<int>>");
|
||||
#endif // !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
#if __cplusplus < 201703L
|
||||
static_assert(!ConversionCompilesFor<span<char>, std::string>,
|
||||
"!ConversionCompilesFor<span<char>, std::string>");
|
||||
#endif
|
||||
static_assert(ConversionCompilesFor<span<const char>, const std::string&>,
|
||||
"ConversionCompilesFor<span<const char>, const std::string&>");
|
||||
static_assert(!ConversionCompilesFor<span<char>, const std::string&>,
|
||||
"!ConversionCompilesFor<span<char>, const std::string&>");
|
||||
static_assert(!ConversionCompilesFor<span<const DerivedClass, 2>, span<DerivedClass>&>,
|
||||
"!ConversionCompilesFor<span<const DerivedClass, 2>, span<DerivedClass>&>");
|
||||
static_assert(!ConversionCompilesFor<span<const DerivedClass, 1>, span<DerivedClass, 2>&>,
|
||||
"!ConversionCompilesFor<span<const DerivedClass, 1>, span<DerivedClass, 2>&>");
|
||||
static_assert(!ConversionCompilesFor<span<const DerivedClass, 3>, span<DerivedClass, 2>&>,
|
||||
"!ConversionCompilesFor<span<const DerivedClass, 3>, span<DerivedClass, 2>&>");
|
||||
static_assert(!ConversionCompilesFor<span<BaseClass, 3>, span<DerivedClass>&>,
|
||||
"!ConversionCompilesFor<span<BaseClass, 3>, span<DerivedClass>&>");
|
||||
static_assert(!ConversionCompilesFor<span<unsigned int>, span<int>&>,
|
||||
"!ConversionCompilesFor<span<unsigned int>, span<int>&>");
|
||||
static_assert(!ConversionCompilesFor<span<const unsigned int>, span<int>&>,
|
||||
"!ConversionCompilesFor<span<const unsigned int>, span<int>&>");
|
||||
static_assert(!ConversionCompilesFor<span<short>, span<int>&>,
|
||||
"!ConversionCompilesFor<span<short>, span<int>&>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, int[4]>,
|
||||
"!ConversionCompilesFor<span<int, 2>, int[4]>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, span<int, 4>>,
|
||||
"!ConversionCompilesFor<span<int, 2>, span<int, 4>>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, span<int>>,
|
||||
"!ConversionCompilesFor<span<int, 2>, span<int>>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 4>, int[2]>,
|
||||
"!ConversionCompilesFor<span<int, 4>, int[2]>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 2>, int[2]>,
|
||||
"!ConversionCompilesFor<span<int, 2>, int[2]>");
|
||||
static_assert(!ConversionCompilesFor<span<int, 4>, span<int, 2>>,
|
||||
"!ConversionCompilesFor<span<int, 4>, span<int, 2>>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool AsWritableBytesCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
AsWritableBytesCompilesFor<U, void_t<decltype(as_writable_bytes(std::declval<U>()))>> = true;
|
||||
static_assert(AsWritableBytesCompilesFor<span<int>>, "AsWriteableBytesCompilesFor<span<int>>");
|
||||
static_assert(!AsWritableBytesCompilesFor<span<const int>>,
|
||||
"!AsWriteableBytesCompilesFor<span<const int>>");
|
||||
|
@ -23,6 +23,13 @@
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
#if __cplusplus >= 201703l
|
||||
using std::void_t;
|
||||
#else // __cplusplus >= 201703l
|
||||
template <class...>
|
||||
using void_t = void;
|
||||
#endif // __cplusplus < 201703l
|
||||
|
||||
// stand-in for a user-defined ref-counted class
|
||||
template <typename T>
|
||||
struct RefCounted
|
||||
@ -57,9 +64,36 @@ bool strict_helper_const(strict_not_null<const int*> p) { return *p == 12; }
|
||||
int* return_pointer() { return nullptr; }
|
||||
} // namespace
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool CtorCompilesFor_A = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
CtorCompilesFor_A<U, void_t<decltype(gsl::strict_not_null<void*>{std::declval<U>()})>> = true;
|
||||
|
||||
template <typename U, int N, typename = void>
|
||||
static constexpr bool CtorCompilesFor_B = false;
|
||||
template <typename U, int N>
|
||||
static constexpr bool CtorCompilesFor_B<U, N, void_t<decltype(gsl::strict_not_null<U>{N})>> = true;
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool DefaultCtorCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool DefaultCtorCompilesFor<U, void_t<decltype(gsl::strict_not_null<U>{})>> = true;
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool CtorCompilesFor_C = false;
|
||||
template <typename U>
|
||||
static constexpr bool CtorCompilesFor_C<
|
||||
U, void_t<decltype(gsl::strict_not_null<U*>{std::declval<std::unique_ptr<U>>()})>> = true;
|
||||
|
||||
TEST(strict_notnull_tests, TestStrictNotNullConstructors)
|
||||
{
|
||||
{
|
||||
static_assert(CtorCompilesFor_A<void*>, "CtorCompilesFor_A<void*>");
|
||||
static_assert(!CtorCompilesFor_A<std::nullptr_t>, "!CtorCompilesFor_A<std::nullptr_t>");
|
||||
static_assert(!CtorCompilesFor_B<void*, 0>, "!CtorCompilesFor_B<void*, 0>");
|
||||
static_assert(!DefaultCtorCompilesFor<void*>, "!DefaultCtorCompilesFor<void*>");
|
||||
static_assert(!CtorCompilesFor_C<int>, "CtorCompilesFor_C<int>");
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
// Forbid non-nullptr assignable types
|
||||
strict_not_null<std::vector<int>> f(std::vector<int>{1});
|
||||
@ -155,6 +189,26 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructors)
|
||||
}
|
||||
}
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool StrictHelperCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
StrictHelperCompilesFor<U, void_t<decltype(strict_helper(std::declval<U>()))>> = true;
|
||||
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool StrictHelperConstCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
StrictHelperConstCompilesFor<U, void_t<decltype(strict_helper_const(std::declval<U>()))>> =
|
||||
true;
|
||||
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool HelperCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool HelperCompilesFor<U, void_t<decltype(helper(std::declval<U>()))>> = true;
|
||||
|
||||
TEST(strict_notnull_tests, TestStrictNotNull)
|
||||
{
|
||||
{
|
||||
@ -164,9 +218,14 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
strict_not_null<int*> snn = &x;
|
||||
#endif
|
||||
static_assert(!StrictHelperCompilesFor<int*>, "!StrictHelperCompilesFor<int*>");
|
||||
static_assert(!StrictHelperConstCompilesFor<int*>,
|
||||
"!StrictHelperCompilesFor<int*>");
|
||||
|
||||
const strict_not_null<int*> snn1{&x};
|
||||
|
||||
static_assert(StrictHelperCompilesFor<const strict_not_null<int*>>,
|
||||
"StrictHelperCompilesFor<const strict_not_null<int*>>");
|
||||
helper(snn1);
|
||||
helper_const(snn1);
|
||||
|
||||
@ -180,9 +239,16 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
strict_not_null<int*> snn = &x;
|
||||
#endif
|
||||
static_assert(!StrictHelperCompilesFor<const int*>, "!StrictHelperFor<const int*>");
|
||||
static_assert(!StrictHelperConstCompilesFor<const int*>,
|
||||
"!StrictHelperCompilesFor<const int*>");
|
||||
|
||||
const strict_not_null<const int*> snn1{&x};
|
||||
|
||||
static_assert(!HelperCompilesFor<const strict_not_null<const int*>>,
|
||||
"!HelperCompilesFor<const strict_not_null<const int*>>");
|
||||
static_assert(StrictHelperConstCompilesFor<const strict_not_null<const int*>>,
|
||||
"StrictHelperCompilesFor<const strict_not_null<const int*>>");
|
||||
helper_const(snn1);
|
||||
|
||||
EXPECT_TRUE(*snn1 == 42);
|
||||
@ -209,6 +275,8 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
||||
strict_not_null<const int*> snn1{&x};
|
||||
const strict_not_null<const int*> snn2{&x};
|
||||
|
||||
static_assert(!StrictHelperCompilesFor<strict_not_null<const int*>>,
|
||||
"!StrictHelperCompilesFor<strict_not_null<const int*>>");
|
||||
strict_helper_const(snn1);
|
||||
strict_helper_const(snn2);
|
||||
|
||||
@ -240,6 +308,8 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
||||
const not_null<const int*> nn1 = snn;
|
||||
const not_null<const int*> nn2{snn};
|
||||
|
||||
static_assert(!HelperCompilesFor<strict_not_null<const int*>>,
|
||||
"!HelperCompilesFor<strict_not_null<const int*>>");
|
||||
helper_const(snn);
|
||||
|
||||
EXPECT_TRUE(snn == nn1);
|
||||
@ -279,6 +349,8 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
||||
const strict_not_null<const int*> snn1{nn};
|
||||
const strict_not_null<const int*> snn2{nn};
|
||||
|
||||
static_assert(!StrictHelperCompilesFor<not_null<const int*>>,
|
||||
"!StrictHelperCompilesFor<not_null<const int*>>");
|
||||
strict_helper_const(nn);
|
||||
|
||||
EXPECT_TRUE(snn1 == nn);
|
||||
@ -318,6 +390,8 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
|
||||
const int i = 42;
|
||||
|
||||
strict_not_null x{&i};
|
||||
static_assert(!HelperCompilesFor<strict_not_null<const int*>>,
|
||||
"!HelperCompilesFor<strict_not_null<const int*>>");
|
||||
helper_const(strict_not_null{&i});
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
@ -339,6 +413,8 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
|
||||
const int* p = &i;
|
||||
|
||||
strict_not_null x{p};
|
||||
static_assert(!HelperCompilesFor<strict_not_null<const int*>>,
|
||||
"!HelperCompilesFor<strict_not_null<const int*>>");
|
||||
helper_const(strict_not_null{p});
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
@ -376,73 +452,3 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
|
||||
#endif
|
||||
}
|
||||
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
#if __cplusplus >= 201703l
|
||||
using std::void_t;
|
||||
#else // __cplusplus >= 201703l
|
||||
template <class...>
|
||||
using void_t = void;
|
||||
#endif // __cplusplus < 201703l
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool CtorCompilesFor_A = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
CtorCompilesFor_A<U, void_t<decltype(gsl::strict_not_null<void*>{std::declval<U>()})>> = true;
|
||||
static_assert(CtorCompilesFor_A<void*>, "CtorCompilesFor_A<void*>");
|
||||
static_assert(!CtorCompilesFor_A<std::nullptr_t>, "!CtorCompilesFor_A<std::nullptr_t>");
|
||||
|
||||
template <typename U, int N, typename = void>
|
||||
static constexpr bool CtorCompilesFor_B = false;
|
||||
template <typename U, int N>
|
||||
static constexpr bool CtorCompilesFor_B<U, N, void_t<decltype(gsl::strict_not_null<U>{N})>> = true;
|
||||
static_assert(!CtorCompilesFor_B<void*, 0>, "!CtorCompilesFor_B<void*, 0>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool CtorCompilesFor_C = false;
|
||||
template <typename U>
|
||||
static constexpr bool CtorCompilesFor_C<
|
||||
U, void_t<decltype(gsl::strict_not_null<U*>{std::declval<std::unique_ptr<U>>()})>> =
|
||||
true;
|
||||
static_assert(!CtorCompilesFor_C<int>, "CtorCompilesFor_C<int>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool DefaultCtorCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool DefaultCtorCompilesFor<U, void_t<decltype(gsl::strict_not_null<U>{})>> = true;
|
||||
static_assert(!DefaultCtorCompilesFor<void*>, "!DefaultCtorCompilesFor<void*>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool StrictHelperCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
StrictHelperCompilesFor<U, void_t<decltype(strict_helper(std::declval<U>()))>> = true;
|
||||
static_assert(StrictHelperCompilesFor<strict_not_null<int*>>,
|
||||
"StrictHelperCompilesFor<strict_not_null<int*>>");
|
||||
static_assert(!StrictHelperCompilesFor<const strict_not_null<const int*>>,
|
||||
"!StrictHelperCompilesFor<const strict_not_null<const int*>>");
|
||||
static_assert(!StrictHelperCompilesFor<const not_null<const int*>>,
|
||||
"!StrictHelperCompilesFor<const not_null<const int*>>");
|
||||
static_assert(!StrictHelperCompilesFor<int*>, "!StrictHelperCompilesFor<int*>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool HelperCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
HelperCompilesFor<U, void_t<decltype(helper(std::declval<U>()))>> = true;
|
||||
static_assert(HelperCompilesFor<not_null<int*>>,
|
||||
"HelperCompilesFor<not_null<int*>>");
|
||||
static_assert(!HelperCompilesFor<const strict_not_null<const int*>>,
|
||||
"!HelperCompilesFor<const strict_not_null<const int*>>");
|
||||
static_assert(!HelperCompilesFor<const not_null<const int*>>,
|
||||
"!HelperCompilesFor<const not_null<const int*>>");
|
||||
static_assert(HelperCompilesFor<int*>, "HelperCompilesFor<int*>");
|
||||
|
||||
template <typename U, typename = void>
|
||||
static constexpr bool StrictHelperConstCompilesFor = false;
|
||||
template <typename U>
|
||||
static constexpr bool
|
||||
StrictHelperConstCompilesFor<U, void_t<decltype(strict_helper_const(std::declval<U>()))>> = true;
|
||||
static_assert(StrictHelperConstCompilesFor<strict_not_null<const int*>>,
|
||||
"StrictHelperCompilesFor<strict_not_null<const int*>>");
|
||||
static_assert(!StrictHelperConstCompilesFor<const int*>, "!StrictHelperCompilesFor<const int*>");
|
||||
|
Loading…
x
Reference in New Issue
Block a user