Skip to content

Commit

Permalink
Fixes #84: C++17 compatibility (but not - the examples are not compil…
Browse files Browse the repository at this point in the history
…ed using C++17):

* Corrected `using` syntax for `conjunction`, `disjunction`, `bool_constant` and `negation`
* Now using `kat::swap` in the swapping loop of `kat::array::swap`
* Now using iterators in the swapping loop of `kat::array::swap`
* Not trying to issue a trap instruction with C++17 in constexpr methods; with C++20 - issuing the instruction when guaranteed evaluation at run-time (and on the device)
* Added: Missing template argument deducation guide for `kat::array`
* Made sure `std::forward` and `std::move` are made accessible with `using` where necessary
* Dropped a copy of an internal libstdc++ definition we were making by mistake
* Avoiding warnings in `span.hpp` about a useless comparison of a 0-valued template argument (which only show up with C++17 enabled)
* Removed inappropriately-placed `KAT_HD` markers within `if constexpr` statements
* Properly marking `std::byte` with its namespace (rather than using just `byte`)
* Properly marking `std::initializer_list` with its namespace (rather than using just `initializer_list`)
* Added: A note about a missing implementation of kat::apply (see issue #86)
* Avoiding a warning about a narrowing cast `int` -> `std::size_t` which the compiler should not be issuing (for i between 0 and 4)
* Moved a type definition out of a doctest sub-testcase - another issue similar to what we had with `tuple_get` (this may already be a problem with C++14)
* Some commented-out code and `#if FALSE`'es related to issue #87 (C++17 + swap troubles)
* Some code formatting tweaks
  • Loading branch information
Eyal Rozenberg committed Nov 23, 2020
1 parent 74d4426 commit 4eff1b7
Show file tree
Hide file tree
Showing 11 changed files with 989 additions and 73 deletions.
9 changes: 4 additions & 5 deletions src/kat/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ template<typename B> struct negation : bool_constant<not bool(B::value)> {};

#else

template <typename ... Bs> using conjunction = std::conjunction<Bs...>;
template <typename ... Bs> using disjunction = std::disjunction<Bs...>;
template <bool B> using bool_constant = std::bool_constant<B>;
template <bool B> using negation = std::negation<B>;

using std::conjunction;
using std::disjunction;
using std::bool_constant;
using std::negation;

#endif

Expand Down
50 changes: 37 additions & 13 deletions src/kat/containers/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@

#include <kat/common.hpp>
#include <kat/detail/range_access.hpp>
#include <kat/utility.hpp>

#include <cstddef>
#include <cstdlib>
#include <array>
#include <tuple>
#include <utility>
#include <algorithm>
#include <iterator>
#include <type_traits>
Expand Down Expand Up @@ -103,20 +103,23 @@ struct array
KAT_FHD CONSTEXPR_SINCE_CPP_14 void fill(const value_type& u)
{
//std::fill_n(begin(), size(), __u);
for(size_type i = 0; i < NumElements; i++) { elements[i] = u; }
for(size_type i = 0; i < NumElements; i++) {
elements[i] = u;
}
}

// Does the noexcept matter here?
KAT_FHD CONSTEXPR_SINCE_CPP_14 void swap(array& other) noexcept(noexcept(std::swap(std::declval<T&>(), std::declval<T&>())))
KAT_FHD CONSTEXPR_SINCE_CPP_14
void
swap(array& other) noexcept(noexcept(std::swap(std::declval<T&>(), std::declval<T&>())))
{
// std::swap_ranges(begin(), end(), other.begin());
for(size_type i = 0; i < NumElements; i++)
{
auto x = elements[i];
auto y = other.elements[i];
elements[i] = y;
other.elements[i] = x;
}
auto first1 = begin();
auto last1 = end();
auto first2 = other.begin();

for (; first1 != last1; ++first1, (void)++first2)
kat::swap(*first1, *first2);
}

// Iterators.
Expand Down Expand Up @@ -151,8 +154,11 @@ struct array
KAT_FHD CONSTEXPR_SINCE_CPP_17 reference at(size_type n) noexcept {
if (n > NumElements) {
#if __CUDA_ARCH__
asm("trap;");
#if __cplusplus >= 202001L
if (not std::is_constant_evaluated()) { asm("trap;"); }
#else
assert(false);
#endif
throw std::out_of_range("kat::array::at: index n exceeds number of elements");
#endif
}
Expand All @@ -165,7 +171,11 @@ struct array
{
if (n > NumElements) {
#if __CUDA_ARCH__
asm("trap;");
#if __cplusplus >= 202001L
if (not std::is_constant_evaluated()) { asm("trap;"); }
#else
assert(false);
#endif
#else
throw std::out_of_range("kat::array::at: index n exceeds number of elements");
#endif
Expand Down Expand Up @@ -194,6 +204,13 @@ struct array
KAT_FHD constexpr const_pointer data() const noexcept { return array_traits_type::pointer(elements); }
};

#if __cpp_deduction_guides >= 201606
template<typename _Tp, typename... _Up>
array(_Tp, _Up...)
-> array<std::enable_if_t<(std::is_same_v<_Tp, _Up> && ...), _Tp>,
1 + sizeof...(_Up)>;
#endif

// Array comparisons.
template<typename T, size_t NumElements>
KAT_FHD CONSTEXPR_SINCE_CPP_17
Expand Down Expand Up @@ -262,7 +279,11 @@ operator>=(const array<T, NumElements>& one, const array<T, NumElements>& two)
}

// Specialized algorithms.
// #if __cplusplus >= 201712L
//template<typename T, size_t NumElements, typename = std::enable_if_t<std::is_swappable<T>::value>>
//#else
template<typename T, size_t NumElements>
// #endif
KAT_FHD CONSTEXPR_SINCE_CPP_14 void swap(array<T, NumElements>& one, array<T, NumElements>& two)
noexcept(noexcept(one.swap(two)))
{
Expand All @@ -279,8 +300,11 @@ KAT_FHD constexpr T& get(array<T, NumElements>& arr) noexcept
template<size_t Integer, typename T, size_t NumElements>
KAT_FHD constexpr T&& get(array<T, NumElements>&& arr) noexcept
{
#ifndef KAT_DEFINE_MOVE_AND_FORWARD
using std::move;
#endif
static_assert(Integer < NumElements, "index is out of bounds");
return std::move(get<Integer>(arr));
return move(get<Integer>(arr));
}

template<size_t Integer, typename T, size_t NumElements>
Expand Down
4 changes: 0 additions & 4 deletions src/kat/containers/detail/normal_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@

#include <type_traits>

#if __cplusplus > 201402L
# define __cpp_lib_array_constexpr 201803
#endif

#if __cplusplus > 201703L
# include <compare>
# include <new>
Expand Down
Loading

0 comments on commit 4eff1b7

Please sign in to comment.