public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Use std::optional when available
@ 2023-10-05 16:05 Tom Tromey
  2023-10-09 12:40 ` Andrew Burgess
  0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2023-10-05 16:05 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This patch changes gdb_optional.h to use std::optional when it is
available.

Note I had to comment out some of the tests.  These give a
set-but-not-used warning when built with the libstdc++ optional.  I
think this change is harmless, particularly because these tests will
be removed entirely once gdb switches to C++17.

Regression tested on x86-64 Fedora 36.
---
 gdb/unittests/optional-selftests.c  |  2 ++
 gdb/unittests/optional/cons/copy.cc |  4 ++++
 gdb/unittests/optional/cons/move.cc |  4 ++++
 gdbsupport/gdb_optional.h           | 17 +++++++++++++++++
 4 files changed, 27 insertions(+)

diff --git a/gdb/unittests/optional-selftests.c b/gdb/unittests/optional-selftests.c
index 8a727c02159..ea8447f4454 100644
--- a/gdb/unittests/optional-selftests.c
+++ b/gdb/unittests/optional-selftests.c
@@ -30,9 +30,11 @@
 /* libstdc++'s testsuite uses VERIFY.  */
 #define VERIFY SELF_CHECK
 
+#if __cplusplus < 201703L
 /* Used to disable testing features not supported by
    gdb::optional.  */
 #define GDB_OPTIONAL
+#endif
 
 namespace selftests {
 namespace optional {
diff --git a/gdb/unittests/optional/cons/copy.cc b/gdb/unittests/optional/cons/copy.cc
index 87a08f9a52b..4b9837dd3c5 100644
--- a/gdb/unittests/optional/cons/copy.cc
+++ b/gdb/unittests/optional/cons/copy.cc
@@ -89,6 +89,9 @@ test ()
 
   enum outcome { nothrow, caught, bad_catch };
 
+  /* These tests give an unused-but-set warning with libstdc++
+     optional.  */
+#ifdef GDB_OPTIONAL
   {
     outcome result = nothrow;
     gdb::optional<throwing_copy> o;
@@ -120,6 +123,7 @@ test ()
 
     VERIFY( result == caught );
   }
+#endif
 
   VERIFY( tracker::count == 0 );
 }
diff --git a/gdb/unittests/optional/cons/move.cc b/gdb/unittests/optional/cons/move.cc
index 398784ae7ec..072c1b83b97 100644
--- a/gdb/unittests/optional/cons/move.cc
+++ b/gdb/unittests/optional/cons/move.cc
@@ -87,6 +87,9 @@ test ()
 
   enum outcome { nothrow, caught, bad_catch };
 
+  /* These tests give an unused-but-set warning with libstdc++
+     optional.  */
+#ifdef GDB_OPTIONAL
   {
     outcome result = nothrow;
     gdb::optional<throwing_move> o;
@@ -118,6 +121,7 @@ test ()
 
     VERIFY( result == caught );
   }
+#endif
 
   VERIFY( tracker::count == 0 );
 }
diff --git a/gdbsupport/gdb_optional.h b/gdbsupport/gdb_optional.h
index 9b7b7b2f7f4..c177691bb65 100644
--- a/gdbsupport/gdb_optional.h
+++ b/gdbsupport/gdb_optional.h
@@ -20,6 +20,21 @@
 #ifndef COMMON_GDB_OPTIONAL_H
 #define COMMON_GDB_OPTIONAL_H
 
+#if __cplusplus >= 201703L
+
+#include <optional>
+
+namespace gdb
+{
+template<typename T> using optional = std::optional<T>;
+
+using in_place_t = std::in_place_t;
+
+inline constexpr in_place_t in_place{};
+}
+
+#else /* __cplusplus < 201703L  */
+
 #include "gdbsupport/traits.h"
 
 namespace gdb
@@ -230,4 +245,6 @@ class optional
 
 }
 
+#endif /* __cplusplus < 201703L */
+
 #endif /* COMMON_GDB_OPTIONAL_H */
-- 
2.40.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-10-16 14:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-05 16:05 [PATCH] Use std::optional when available Tom Tromey
2023-10-09 12:40 ` Andrew Burgess
2023-10-10 16:49   ` Tom Tromey
2023-10-12 18:44     ` Pedro Alves
2023-10-16 14:13     ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).