[binutils-gdb] Make gdb::span conform more closely to C++20
Tom Tromey
tromey@sourceware.org
Thu Nov 19 16:26:20 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c1c5ee61564308560d3016925eb025ff950a5b4a
commit c1c5ee61564308560d3016925eb025ff950a5b4a
Author: Tom Tromey <tromey@adacore.com>
Date: Tue Nov 3 14:05:09 2026 -0700
Make gdb::span conform more closely to C++20
This removes the single-object constructors from gdb::span, making it
conform more closely to C++20. Also, the cbegin and cend methods are
removed. These do not appear until C++23.
These changes necessitate some unit test changes as well.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31422
Approved-By: Simon Marchi <simon.marchi@efficios.com>
Diff:
---
gdb/unittests/span-selftests.c | 35 ++++++-----------------------------
gdbsupport/span.h | 28 +---------------------------
2 files changed, 7 insertions(+), 56 deletions(-)
diff --git a/gdb/unittests/span-selftests.c b/gdb/unittests/span-selftests.c
index 2cf875d187c..2317f34d1d6 100644
--- a/gdb/unittests/span-selftests.c
+++ b/gdb/unittests/span-selftests.c
@@ -56,13 +56,13 @@ using gdb::span;
/* immutable span */
static_assert ( is_convertible<const T (&) [1], span<const T>> ());
static_assert ( is_convertible<T (&) [1], span<const T>> ());
-static_assert ( is_convertible<const T, span<const T>> ());
-static_assert ( is_convertible<T, span<const T>> ());
+static_assert (!is_convertible<const T, span<const T>> ());
+static_assert (!is_convertible<T, span<const T>> ());
/* mutable span */
static_assert ( is_convertible<T (&) [1], span<T>> ());
static_assert (!is_convertible<const T (&) [1], span<T>> ());
-static_assert ( is_convertible<T, span<T>> ());
+static_assert (!is_convertible<T, span<T>> ());
static_assert (!is_convertible<const T, span<T>> ());
/* While float is implicitly convertible to gdb_byte, we don't want
@@ -93,12 +93,12 @@ static_assert (!is_convertible <C (&)[1], span<B>> ());
/* Same, for elem->view. */
-static_assert ( is_convertible <A, span<A>> ());
+static_assert (!is_convertible <A, span<A>> ());
static_assert (!is_convertible <B, span<A>> ());
static_assert (!is_convertible <C, span<A>> ());
static_assert (!is_convertible <A, span<B>> ());
-static_assert ( is_convertible <B, span<B>> ());
+static_assert (!is_convertible <B, span<B>> ());
static_assert (!is_convertible <C, span<B>> ());
/* Check that there's no container->view conversion for containers of derived
@@ -369,21 +369,6 @@ check_iterator ()
SELF_CHECK (sum == 2 + 4 + 6 + 8);
}
-template<typename T>
-static void
-check_const_iterator ()
-{
- T data[] = {1, 2, 3, 4};
- gdb::span<T> view (data);
-
- typename std::decay<T>::type sum = 0;
- for (typename gdb::span<T>::const_iterator it = view.cbegin ();
- it != view.cend (); it++)
- sum += *it;
-
- SELF_CHECK (sum == 1 + 2 + 3 + 4);
-}
-
/* Entry point. */
static void
@@ -421,7 +406,7 @@ run_tests ()
SELF_CHECK (check_container_view (cview, array));
}
- /* op=(std::vector/std::array/elem) */
+ /* op=(std::vector/std::array) */
{
gdb::span<gdb_byte> view;
@@ -430,12 +415,6 @@ run_tests ()
view = array;
SELF_CHECK (check_container_view (view, array));
-
- gdb_byte elem = 0;
- view = gdb::make_span (&elem, 1);
- SELF_CHECK (check_elem_view (view, elem));
- view = std::move (gdb::make_span (&elem, 1));
- SELF_CHECK (check_elem_view (view, elem));
}
/* Test copy/move ctor and mutable->immutable conversion. */
@@ -507,8 +486,6 @@ run_tests ()
check_range_for<gdb_byte> ();
check_range_for<const gdb_byte> ();
check_iterator<gdb_byte> ();
- check_const_iterator<gdb_byte> ();
- check_const_iterator<const gdb_byte> ();
/* Check that the right ctor overloads are taken when the element is
a container. */
diff --git a/gdbsupport/span.h b/gdbsupport/span.h
index 483fffb30fc..67cb8fbb36e 100644
--- a/gdbsupport/span.h
+++ b/gdbsupport/span.h
@@ -33,10 +33,7 @@
The main intent of span is to use it as function input
parameter type, making it possible to pass in any sequence of
contiguous objects, irrespective of whether the objects live on the
- stack or heap and what actual container owns them. Implicit
- construction from the element type is supported too, making it easy
- to call functions that expect an array of elements when you only
- have one element (usually on the stack). For example:
+ stack or heap and what actual container owns them. For example:
struct A { .... };
void function (gdb::span<A> as);
@@ -44,12 +41,10 @@
std::vector<A> std_vec = ...;
std::array<A, N> std_array = ...;
A array[] = {...};
- A elem;
function (std_vec);
function (std_array);
function (array);
- function (elem);
Views can be either mutable or const. A const view is simply
created by specifying a const T as span template parameter,
@@ -88,7 +83,6 @@ public:
using reference = T &;
using const_reference = const T &;
using size_type = size_t;
- using const_iterator = const T *;
using iterator = T *;
/* Default construction creates an empty view. */
@@ -96,24 +90,6 @@ public:
: m_array (nullptr), m_size (0)
{}
- /* Create a span over a single object of the type of an
- span element. The created view as size==1. This is
- templated on U to allow constructing a span<const T> over a
- (non-const) T. The "convertible" requirement makes sure that you
- can't create a span<T> over a const T. */
- template<typename U,
- typename = Requires<DecayedConvertible<U>>>
- constexpr span (U &elem) noexcept
- : m_array (&elem), m_size (1)
- {}
-
- /* Same as above, for rvalue references. */
- template<typename U,
- typename = Requires<DecayedConvertible<U>>>
- constexpr span (U &&elem) noexcept
- : m_array (&elem), m_size (1)
- {}
-
/* Create a span from a pointer to an array and an element
count. */
template<typename U,
@@ -172,10 +148,8 @@ public:
constexpr const T *data () const noexcept { return m_array; }
constexpr iterator begin () const noexcept { return m_array; }
- constexpr const_iterator cbegin () const noexcept { return m_array; }
constexpr iterator end () const noexcept { return m_array + m_size; }
- constexpr const_iterator cend () const noexcept { return m_array + m_size; }
constexpr reference operator[] (size_t index) noexcept
{
More information about the Gdb-cvs
mailing list