From f212affd74b640d10cc086ccebeec74d425bb908 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Steffen=20M=C3=BCthing?= <muething@dune-project.org>
Date: Fri, 18 Aug 2017 11:20:50 +0200
Subject: [PATCH] [cmake] Add tests for some advanced constexpr features

---
 cmake/modules/CheckCXXFeatures.cmake | 41 ++++++++++++++++++++++++++++
 config.h.cmake                       |  6 ++++
 2 files changed, 47 insertions(+)

diff --git a/cmake/modules/CheckCXXFeatures.cmake b/cmake/modules/CheckCXXFeatures.cmake
index cd3d99d57..5c4bd828c 100644
--- a/cmake/modules/CheckCXXFeatures.cmake
+++ b/cmake/modules/CheckCXXFeatures.cmake
@@ -440,3 +440,44 @@ if(NOT STDTHREAD_WORKS)
     "STDTHREAD_LINK_FLAGS.  If you think this test is wrong, set the cache "
     "variable STDTHREAD_WORKS.")
 endif(NOT STDTHREAD_WORKS)
+
+
+# Make sure we have working generalized constexpr - GCC 4.9 lacks this feature
+check_cxx_source_compiles("
+  constexpr int foo(int bar)
+  {
+    int r = 1;
+    for (int i = 0 ; i < bar ; ++i)
+      r += r;
+    return r;
+  }
+
+  int main()
+  {
+    static_assert(foo(4) == 16, \"test failed\");
+    return 0;
+  }
+" DUNE_HAVE_CXX_GENERALIZED_CONSTEXPR
+  )
+
+
+# Check whether we can conditionally throw exceptions in constexpr context to
+# signal errors both at compile time and at run time - this does not work in GCC 5
+check_cxx_source_compiles("
+  constexpr int foo(int bar)
+  {
+    if (bar < 0)
+      throw bar;
+    int r = 1;
+    for (int i = 0 ; i < bar ; ++i)
+      r += r;
+    return r;
+  }
+
+  int main()
+  {
+    static_assert(foo(4) == 16, \"test failed\");
+    return 0;
+  }
+" DUNE_SUPPORTS_CXX_THROW_IN_CONSTEXPR
+  )
diff --git a/config.h.cmake b/config.h.cmake
index 33f7bd3e6..8cf20772f 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -29,6 +29,12 @@
 /* does the compiler support C++17's class template argument deduction? */
 #cmakedefine DUNE_HAVE_CXX_CLASS_TEMPLATE_ARGUMENT_DEDUCTION 1
 
+/* does the compiler support C++14's generalized constant expressions? */
+#cmakedefine DUNE_HAVE_CXX_GENERALIZED_CONSTEXPR 1
+
+/* does the compiler support conditionally throwing exceptions in constexpr context? */
+#cmakedefine DUNE_SUPPORTS_CXX_THROW_IN_CONSTEXPR 1
+
 /* Define if you have a BLAS library. */
 #cmakedefine HAVE_BLAS 1
 
-- 
GitLab