[PATCH] libstdc++: Define std::nothrow as inline variable
Jonathan Wakely
jwakely@redhat.com
Mon Jul 14 20:40:26 GMT 2025
This makes it possible to use `new(std::nothrow) X` without linking to
libsupc++ or libstdc++.
To ensure we still export the symbol from the library we need to
suppress the inline variable in libsupc++/new_handler.cc which is done
by defining a macro.
libstdc++-v3/ChangeLog:
* libsupc++/new (nothrow): Define as inline variable.
* libsupc++/new_handler.cc (_GLIBCXX_DEFINE_NOTHROW_OBJ):
Define.
---
Tested powerpc64le-linux.
libstdc++-v3/libsupc++/new | 4 ++++
libstdc++-v3/libsupc++/new_handler.cc | 2 ++
2 files changed, 6 insertions(+)
diff --git a/libstdc++-v3/libsupc++/new b/libstdc++-v3/libsupc++/new
index fb36dae25a6d..85d28ff40769 100644
--- a/libstdc++-v3/libsupc++/new
+++ b/libstdc++-v3/libsupc++/new
@@ -125,7 +125,11 @@ namespace std
#endif
};
+#if defined __cpp_inline_variables && ! defined _GLIBCXX_DEFINE_NOTHROW_OBJ
+ inline constexpr nothrow_t nothrow{};
+#else
extern const nothrow_t nothrow;
+#endif
/** If you write your own error handler to be called by @c new, it must
* be of this type. */
diff --git a/libstdc++-v3/libsupc++/new_handler.cc b/libstdc++-v3/libsupc++/new_handler.cc
index 7cd3e5a69fde..96dfb796c64a 100644
--- a/libstdc++-v3/libsupc++/new_handler.cc
+++ b/libstdc++-v3/libsupc++/new_handler.cc
@@ -23,6 +23,8 @@
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
+#define _GLIBCXX_DEFINE_NOTHROW_OBJ 1
+
#include "new"
#include <bits/atomic_lockfree_defines.h>
--
2.50.1
More information about the Libstdc++
mailing list