Skip to content

Commit 162836b

Browse files
falucocodebot
authored andcommitted
adt: Fix wrong assertion in span::subspan
While there, upgrade span class to c++17
1 parent 7ea94bc commit 162836b

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

include/srsran/adt/span.h

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "fmt/format.h"
1515
#include <algorithm>
1616
#include <array>
17-
#include <cassert>
1817
#include <iterator>
1918
#include <type_traits>
2019
#include <vector>
@@ -28,14 +27,6 @@ namespace detail {
2827

2928
/// Helper traits used by SFINAE expressions in constructors.
3029

31-
template <typename... Ts>
32-
struct make_void {
33-
typedef void type;
34-
};
35-
36-
template <typename... Ts>
37-
using void_t = typename make_void<Ts...>::type;
38-
3930
template <typename U>
4031
struct is_span : std::false_type {
4132
};
@@ -51,7 +42,7 @@ struct is_std_array<std::array<U, N>> : std::true_type {
5142
};
5243

5344
template <typename U>
54-
using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<U>::type>::type;
45+
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<U>>;
5546

5647
template <class Container, class U, class = void>
5748
struct is_container_compatible : public std::false_type {
@@ -60,21 +51,20 @@ template <class Container, class U>
6051
struct is_container_compatible<
6152
Container,
6253
U,
63-
void_t<
54+
std::void_t<
6455
// Check if the container type has data and size members.
6556
decltype(std::declval<Container>().data()),
6657
decltype(std::declval<Container>().size()),
6758
// Container should not be a span.
68-
typename std::enable_if<!is_span<remove_cvref_t<Container>>::value, int>::type,
59+
std::enable_if_t<!is_span<remove_cvref_t<Container>>::value, int>,
6960
// Container should not be a std::array.
70-
typename std::enable_if<!is_std_array<remove_cvref_t<Container>>::value, int>::type,
61+
std::enable_if_t<!is_std_array<remove_cvref_t<Container>>::value, int>,
7162
// Container should not be an array.
72-
typename std::enable_if<!std::is_array<remove_cvref_t<Container>>::value, int>::type,
63+
std::enable_if_t<!std::is_array_v<remove_cvref_t<Container>>, int>,
7364
// Check type compatibility between the contained type and the span type.
74-
typename std::enable_if<
75-
std::is_convertible<typename std::remove_pointer<decltype(std::declval<Container>().data())>::type (*)[],
76-
U (*)[]>::value,
77-
int>::type>> : public std::true_type {
65+
std::enable_if_t<
66+
std::is_convertible_v<std::remove_pointer_t<decltype(std::declval<Container>().data())> (*)[], U (*)[]>,
67+
int>>> : public std::true_type {
7868
};
7969

8070
} // namespace detail
@@ -87,7 +77,7 @@ class span
8777
public:
8878
/// Member types.
8979
using element_type = T;
90-
using value_type = typename std::remove_cv<T>::type;
80+
using value_type = std::remove_cv_t<T>;
9181
using size_type = std::size_t;
9282
using difference_type = std::ptrdiff_t;
9383
using pointer = element_type*;
@@ -100,6 +90,9 @@ class span
10090
/// Constructs an empty span with data() == nullptr and size() == 0.
10191
constexpr span() noexcept = default;
10292

93+
constexpr span(const span&) noexcept = default;
94+
span& operator=(const span& other) noexcept = default;
95+
10396
/// Constructs a span that is a view over the range [ptr, ptr + len).
10497
constexpr span(pointer ptr_, size_type len_) noexcept : ptr(ptr_), len(len_) {}
10598

@@ -113,45 +106,38 @@ class span
113106
}
114107

115108
/// Constructs a span that is a view over the array arr.
116-
template <typename U,
117-
std::size_t N,
118-
typename std::enable_if<std::is_convertible<U (*)[], element_type (*)[]>::value, int>::type = 0>
109+
template <typename U, std::size_t N, std::enable_if_t<std::is_convertible_v<U (*)[], element_type (*)[]>, int> = 0>
119110
constexpr span(std::array<U, N>& arr) noexcept : ptr(arr.data()), len(N)
120111
{
121112
}
122113

123114
/// Constructs a span that is a view over the array arr.
124115
template <typename U,
125116
std::size_t N,
126-
typename std::enable_if<std::is_convertible<const U (*)[], element_type (*)[]>::value, int>::type = 0>
117+
std::enable_if_t<std::is_convertible_v<const U (*)[], element_type (*)[]>, int> = 0>
127118
constexpr span(const std::array<U, N>& arr) noexcept : ptr(arr.data()), len(N)
128119
{
129120
}
130121

131122
/// Constructs a span that is a view over the container c.
132123
template <typename Container,
133-
typename std::enable_if<detail::is_container_compatible<Container, element_type>::value, int>::type = 0>
124+
std::enable_if_t<detail::is_container_compatible<Container, element_type>::value, int> = 0>
134125
constexpr span(Container& container) noexcept : ptr(container.data()), len(container.size())
135126
{
136127
}
137128

138129
/// Constructs a span that is a view over the container c.
139-
template <
140-
typename Container,
141-
typename std::enable_if<detail::is_container_compatible<const Container, element_type>::value, int>::type = 0>
130+
template <typename Container,
131+
std::enable_if_t<detail::is_container_compatible<const Container, element_type>::value, int> = 0>
142132
constexpr span(const Container& container) noexcept : ptr(container.data()), len(container.size())
143133
{
144134
}
145135

146-
template <typename U, typename std::enable_if<std::is_convertible<U (*)[], element_type (*)[]>::value, int>::type = 0>
136+
template <typename U, std::enable_if_t<std::is_convertible_v<U (*)[], element_type (*)[]>, int> = 0>
147137
constexpr span(const span<U>& other) noexcept : ptr(other.data()), len(other.size())
148138
{
149139
}
150140

151-
span& operator=(const span& other) noexcept = default;
152-
153-
~span() noexcept = default;
154-
155141
/// Returns the number of elements in the span.
156142
constexpr size_type size() const noexcept { return len; }
157143

@@ -219,7 +205,8 @@ class span
219205
/// Obtains a span that is a view over the count elements of this span starting at offset offset.
220206
span<element_type> subspan(size_type offset, size_type count) const
221207
{
222-
srsran_assert(count <= size() - offset, "Size out of bounds!");
208+
srsran_assert(offset <= size(), "Offset is out of bounds!");
209+
srsran_assert(count <= size() - offset, "Offset plus size is out of bounds!");
223210
return {data() + offset, count};
224211
}
225212

0 commit comments

Comments
 (0)