Skip to content

Commit

Permalink
Fix MSVC 2017 having issues with constexpr functions
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Jul 7, 2023
1 parent 56b7539 commit 4416f2a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,32 @@ inline constexpr bool
has_no_iter_difference_t<T, cuda::std::void_t<cuda::std::iter_difference_t<T>>> = false;
#endif

template <class T, class Expected>
__host__ __device__ constexpr bool check_iter_difference_t() {
constexpr bool result = cuda::std::same_as<cuda::std::iter_difference_t<T>, Expected>;
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T const>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T volatile>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T const volatile>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T const&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T volatile&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T const volatile&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T const&&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T volatile&&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_difference_t<T const volatile&&>, Expected> == result);
// Old MSVC has big trouble compiling these at compile time otherwise
template <class T, class Expected,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T const>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T volatile>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T const volatile>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T const&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T volatile&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T const volatile&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T const&&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T volatile&&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_difference_t<T const volatile&&>, Expected>>
inline constexpr bool check_iter_difference_t = false;

return result;
}
template <class T, class Expected>
inline constexpr bool check_iter_difference_t<T, Expected,
true, true, true, true, true,
true, true, true, true, true> = true;

static_assert(check_iter_difference_t<int, int>());
static_assert(check_iter_difference_t<int*, cuda::std::ptrdiff_t>());
static_assert(check_iter_difference_t<int, int>);
static_assert(check_iter_difference_t<int*, cuda::std::ptrdiff_t>);

struct int_subtraction {
__host__ __device__ friend int operator-(int_subtraction, int_subtraction);
};
static_assert(check_iter_difference_t<int_subtraction, int>());
static_assert(check_iter_difference_t<int_subtraction, int>);

static_assert(has_no_iter_difference_t<void>);
static_assert(has_no_iter_difference_t<double>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,36 @@

#include <cuda/std/concepts>

template <class T, class Expected>
__host__ __device__ constexpr bool check_iter_value_t() {
constexpr bool result = cuda::std::same_as<cuda::std::iter_value_t<T>, Expected>;
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T const>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T volatile>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T const volatile>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T const&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T volatile&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T const volatile&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T const&&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T volatile&&>, Expected> == result);
static_assert(cuda::std::same_as<cuda::std::iter_value_t<T const volatile&&>, Expected> == result);
using wh = cuda::std::iter_value_t<int*>;

return result;
}
// Old MSVC has big trouble compiling these at compile time otherwise
template <class T, class Expected,
bool = cuda::std::same_as<cuda::std::iter_value_t<T>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T const>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T volatile>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T const volatile>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T const&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T volatile&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T const volatile&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T const&&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T volatile&&>, Expected>,
bool = cuda::std::same_as<cuda::std::iter_value_t<T const volatile&&>, Expected>>
inline constexpr bool check_iter_value_t = false;

static_assert(check_iter_value_t<int*, int>());
static_assert(check_iter_value_t<int[], int>());
static_assert(check_iter_value_t<int[10], int>());
template <class T, class Expected>
inline constexpr bool check_iter_value_t<T, Expected,
true, true, true, true, true,
true, true, true, true, true> = true;

static_assert(check_iter_value_t<int*, int>);
static_assert(check_iter_value_t<int[], int>);
static_assert(check_iter_value_t<int[10], int>);

struct both_members {
using value_type = double;
using element_type = double;
};
static_assert(check_iter_value_t<both_members, double>());
static_assert(check_iter_value_t<both_members, double>);

// clang-format off
template <class T, class = void>
Expand All @@ -61,4 +66,6 @@ struct different_value_element_members {
};
static_assert(check_no_iter_value_t<different_value_element_members>);

int main(int, char**) { return 0; }
int main(int, char**) {
return 0;
}

0 comments on commit 4416f2a

Please sign in to comment.