From 277db90f71da6a905807df02b67541a912e1ecd8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Steffen=20M=C3=BCthing?= <muething@dune-project.org>
Date: Tue, 27 Aug 2013 14:26:26 +0200
Subject: [PATCH] Check for std::conditional and pull it into the Dune
 namespace

If not found, our own fallback implementation will be used.
---
 cmake/modules/CMakeLists.txt             |  1 +
 cmake/modules/DuneMacros.cmake           |  1 +
 cmake/modules/FindCXX11Conditional.cmake | 17 ++++++++++++++++
 cmake/modules/Makefile.am                |  1 +
 config.h.cmake                           |  3 +++
 dune/common/typetraits.hh                | 12 +++++++++++-
 m4/CMakeLists.txt                        |  1 +
 m4/Makefile.am                           |  1 +
 m4/cxx11_conditional.m4                  | 25 ++++++++++++++++++++++++
 m4/dune_common.m4                        |  1 +
 10 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 cmake/modules/FindCXX11Conditional.cmake
 create mode 100644 m4/cxx11_conditional.m4

diff --git a/cmake/modules/CMakeLists.txt b/cmake/modules/CMakeLists.txt
index 00e6852a1..cee14ed1c 100644
--- a/cmake/modules/CMakeLists.txt
+++ b/cmake/modules/CMakeLists.txt
@@ -10,6 +10,7 @@ set(modules DuneBoost.cmake
   DuneTestMacros.cmake
   DuneTests.cmake
   FindBoostFusion.cmake
+  FindCXX11Conditional.cmake
   FindCXX11Features.cmake
   FindGMP.cmake
   FindInkscape.cmake
diff --git a/cmake/modules/DuneMacros.cmake b/cmake/modules/DuneMacros.cmake
index 51da973d8..ea12136cc 100644
--- a/cmake/modules/DuneMacros.cmake
+++ b/cmake/modules/DuneMacros.cmake
@@ -419,6 +419,7 @@ macro(dune_project)
 
   # set required compiler flags for C++11 (former C++0x)
   find_package(CXX11Features)
+  find_package(CXX11Conditional)
 
   include(DuneCxaDemangle)
 
diff --git a/cmake/modules/FindCXX11Conditional.cmake b/cmake/modules/FindCXX11Conditional.cmake
new file mode 100644
index 000000000..6c85ca65e
--- /dev/null
+++ b/cmake/modules/FindCXX11Conditional.cmake
@@ -0,0 +1,17 @@
+# Module that checks whether the compiler supports
+# C++11 std::conditional.
+#
+# Sets the following variable:
+# HAVE_STD_CONDITIONAL
+#
+# perform tests
+include(CheckCXXSourceCompiles)
+
+check_cxx_source_compiles("
+
+  #include <type_traits>
+
+  int main(void){
+      return std::conditional<true,std::integral_constant<int,0>,void>::type::value;
+  }"
+  HAVE_STD_CONDITIONAL)
diff --git a/cmake/modules/Makefile.am b/cmake/modules/Makefile.am
index 04631b7dd..7870a68fa 100644
--- a/cmake/modules/Makefile.am
+++ b/cmake/modules/Makefile.am
@@ -10,6 +10,7 @@ MODULES = DuneBoost.cmake \
   DuneTestMacros.cmake    \
   DuneTests.cmake         \
   FindBoostFusion.cmake   \
+  FindCXX11Conditional.cmake \
   FindCXX11Features.cmake \
   FindGMP.cmake           \
   FindInkscape.cmake      \
diff --git a/config.h.cmake b/config.h.cmake
index 1ded4db4d..af32e048e 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -108,6 +108,9 @@
 /* Define to 1 if you have the <type_traits> header file. */
 #cmakedefine HAVE_TYPE_TRAITS 1
 
+/* Define to 1 if you have the <type_traits> header file. */
+#cmakedefine HAVE_STD_CONDITIONAL 1
+
 /* Define to 1 if the MPI2 Standard is supported */
 #cmakedefine MPI_2 1
 
diff --git a/dune/common/typetraits.hh b/dune/common/typetraits.hh
index 1d08eb286..4763ed9b5 100644
--- a/dune/common/typetraits.hh
+++ b/dune/common/typetraits.hh
@@ -395,6 +395,8 @@ namespace Dune
     typedef T2 Type DUNE_DEPRECATED_MSG("Use Dune::conditional::type instead");
   };
 
+#if DOXYGEN || !HAVE_STD_CONDITIONAL
+
    /**
    * @brief Select a type based on a condition.
    *
@@ -403,7 +405,8 @@ namespace Dune
    * The selected type is accessible through the typedef
    * type.
    *
-   * \note This is a reimplementation of the C++11 stl feature of the same name.
+   * \note If available, this uses C++11 std::conditional, otherwise it provides
+   *       a reimplementation.
    */
   template<bool first, class T1, class T2>
   struct conditional
@@ -423,6 +426,13 @@ namespace Dune
     typedef T2 type;
   };
 
+#else // DOXYGEN || !HAVE_STD_CONDITONAL
+
+  // pull in default implementation
+  using std::conditional;
+
+#endif // DOXYGEN || !HAVE_STD_CONDITONAL
+
   ////////////////////////////////////////////////////////////////////////
   //
   // integral_constant (C++0x 20.7.3 "Helper classes")
diff --git a/m4/CMakeLists.txt b/m4/CMakeLists.txt
index 5a842931e..40c791eb1 100644
--- a/m4/CMakeLists.txt
+++ b/m4/CMakeLists.txt
@@ -13,6 +13,7 @@ install(PROGRAMS
         cxx0x_static_assert.m4
         cxx0x_variadic.m4
         cxx0x_variadic_constructor_sfinae.m4
+        cxx11_conditional.m4
         dune.m4
         dune_all.m4
         dune_autobuild.m4
diff --git a/m4/Makefile.am b/m4/Makefile.am
index 68a0b3f51..32fbabbd5 100644
--- a/m4/Makefile.am
+++ b/m4/Makefile.am
@@ -17,6 +17,7 @@ ALLM4S = 					\
 	cxx0x_static_assert.m4			\
 	cxx0x_variadic.m4			\
 	cxx0x_variadic_constructor_sfinae.m4    \
+	cxx11_conditional.m4			\
 	dune.m4					\
 	dune_all.m4				\
 	dune_autobuild.m4			\
diff --git a/m4/cxx11_conditional.m4 b/m4/cxx11_conditional.m4
new file mode 100644
index 000000000..8bbe0c874
--- /dev/null
+++ b/m4/cxx11_conditional.m4
@@ -0,0 +1,25 @@
+# tests for C++11 conditional support
+# the associated macro is called HAVE_STD_CONDITIONAL
+
+AC_DEFUN([CXX11_CONDITIONAL_CHECK],[
+  AC_CACHE_CHECK([for C++11 std::conditional], dune_cv_cxx11_conditional_support, [
+    AC_REQUIRE([AC_PROG_CXX])
+    AC_REQUIRE([GXX0X])
+    AC_LANG_PUSH([C++])
+    AC_RUN_IFELSE([
+      AC_LANG_PROGRAM([
+
+        #include <type_traits>
+
+        ],
+        [
+          return std::conditional<true,std::integral_constant<int,0>,void>::type::value;
+        ])],
+      dune_cv_cxx11_conditional_support=yes,
+      dune_cv_cxx11_conditional_support=no)
+    AC_LANG_POP
+  ])
+  if test "x$dune_cv_cxx11_conditional_support" = xyes; then
+    AC_DEFINE(HAVE_STD_CONDITIONAL, 1, [Define to 1 if C++11 std::conditional is supported])
+  fi
+])
diff --git a/m4/dune_common.m4 b/m4/dune_common.m4
index 651a23e09..639397b43 100644
--- a/m4/dune_common.m4
+++ b/m4/dune_common.m4
@@ -24,6 +24,7 @@ AC_DEFUN([DUNE_COMMON_CHECKS],
   AC_REQUIRE([SHARED_PTR])
   AC_REQUIRE([VARIADIC_TEMPLATES_CHECK])
   AC_REQUIRE([RVALUE_REFERENCES_CHECK])
+  AC_REQUIRE([CXX11_CONDITIONAL_CHECK])
   AC_REQUIRE([DUNE_BOOST_BASE])
   AC_REQUIRE([MAKE_SHARED])
   AC_REQUIRE([DUNE_LINKCXX])
-- 
GitLab