Browse Source

use exception specifiers only for C++ versions older than C++11

Tested using GCC 5.2.1.

No exception specifiers (throw(...)) fail with -std=c++98 -fexceptions.
Omitting noexcept (or throw()) fails with -std=c++11 and -std=c++14.

Using __cplusplus to get C++ standard version which defines
how the definitions in the standard library look like
and using GRASS_MM_USE_EXCEPTION_SPECIFIER we then use the right ones.

This contains old fix for -fexceptions with GCC 4.7 (see https://trac.osgeo.org/grass/ticket/1533, https://trac.osgeo.org/grass/changeset/50130)
and new fix for GCC 6 where -std=gnu++14 is by default (see https://trac.osgeo.org/grass/ticket/2871
and Debian Bug 811886).

Works also with clang++ -std=c++14.


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@68818 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 8 years ago
parent
commit
b4870b2372
2 changed files with 32 additions and 4 deletions
  1. 14 2
      include/iostream/mm.h
  2. 18 2
      lib/iostream/mm.cpp

+ 14 - 2
include/iostream/mm.h

@@ -39,6 +39,11 @@
 
 #include <sys/types.h>
 
+// GCC with C++98 and -fexceptions requires exception
+// specifiers, however with C++11 and newer, using them causes an error.
+#if __cplusplus < 201103L
+#define GRASS_MM_USE_EXCEPTION_SPECIFIER
+#endif /* __cplusplus < 201103L */
 
 #define MM_REGISTER_VERSION 2
 
@@ -128,10 +133,17 @@ public:
   void print();
 
   friend class mm_register_init;
-  friend void * operator new(size_t) throw(std::bad_alloc);
-  friend void * operator new[](size_t) throw(std::bad_alloc);
+#ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
+  friend void * operator new(size_t) throw (std::bad_alloc);
+  friend void * operator new[] (size_t) throw (std::bad_alloc);
   friend void operator delete(void *) throw();
   friend void operator delete[](void *) throw();
+#else
+  friend void * operator new(size_t);
+  friend void * operator new[] (size_t);
+  friend void operator delete(void *) noexcept;
+  friend void operator delete[](void *) noexcept;
+#endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
 };
 
 

+ 18 - 2
lib/iostream/mm.cpp

@@ -276,7 +276,11 @@ MM_err MM_register::register_deallocation(size_t sz) {
 
  
 /* ************************************************************ */
-void* operator new[] (size_t sz) throw(std::bad_alloc) {
+#ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
+void* operator new[] (size_t sz) throw (std::bad_alloc) {
+#else
+void* operator new[] (size_t sz) {
+#endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
   void *p;
   
   MM_DEBUG cout << "new: sz=" << sz << ", register " 
@@ -327,7 +331,11 @@ void* operator new[] (size_t sz) throw(std::bad_alloc) {
 
  
 /* ************************************************************ */
-void* operator new (size_t sz) throw(std::bad_alloc) {
+#ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
+void* operator new (size_t sz) throw (std::bad_alloc) {
+#else
+void* operator new (size_t sz) {
+#endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
   void *p;
   
   MM_DEBUG cout << "new: sz=" << sz << ", register " 
@@ -379,7 +387,11 @@ void* operator new (size_t sz) throw(std::bad_alloc) {
 
 
 /* ---------------------------------------------------------------------- */
+#ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
 void operator delete (void *ptr) throw() {
+#else
+void operator delete (void *ptr) noexcept {
+#endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
   size_t sz;
   void *p;
   
@@ -419,7 +431,11 @@ void operator delete (void *ptr) throw() {
 
 
 /* ---------------------------------------------------------------------- */
+#ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
 void operator delete[] (void *ptr) throw() {
+#else
+void operator delete[] (void *ptr) noexcept {
+#endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
   size_t sz;
   void *p;