From b65e1e89b46eb32e7321c0fe28683797c7ad5bf3 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Thu, 8 Dec 2016 00:14:39 +0000
Subject: [PATCH 2/2] macOS: When target lt 10.9, libstdc++ supports only some
 C++11

.. <type_traits> and std::function are not available, except via
tr1. We prevent Rcpp from considering this combination as C++11.

Without this, hunspell cannot be built. Interestingly, the compiler
does a bad job of reporting no <type_traits> but --save-temps will
reveals this to be the crux of the issue. We use <tr1/type_traits>
here.

readxl fails due to not having std::function. Again, we  use the
tr1 implementation in <tr1/functional> instead.

I do a cheeky import of the tr1 namespace into the std namespace
here so that we can continue to use `::std::foo`, but better (and
also for the pre-existing TR1 wrappers which currently #include
headers directly into compiler.h (slowing down all compilation)
would be to use namespace aliases for each HAS_TR1 as follows:

inst/include/Rcpp/platform/compiler.h:

namespace ::std {
  namespace ::tr1 {
  {
  }
}
namespace ns_std_functional = ::std::tr1;
namespace ns_std_functional = ::std;

.. then the rest of the code can use ns_std_functional and we only
pay the #include for the actual header cost when the code is needed.

.. you still need #ifdef HAS_TR1_FUNCTIONAL in the public headers so
you #include the right <tr1/functional> vs. <functional>, though you
could move all this logic into those public headers if you prefer.

In summary, it is cleaner than my namespace import hack and faster
than using #include in compiler.h
---
 inst/include/Rcpp/InternalFunction.h                |  4 ++--
 inst/include/Rcpp/InternalFunctionWithStdFunction.h |  7 ++++++-
 inst/include/Rcpp/platform/compiler.h               | 11 ++++++++++-
 inst/include/Rcpp/sugar/functions/sapply.h          |  9 +++++++--
 4 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/inst/include/Rcpp/InternalFunction.h b/inst/include/Rcpp/InternalFunction.h
index 5a5722b..56922b2 100644
--- a/inst/include/Rcpp/InternalFunction.h
+++ b/inst/include/Rcpp/InternalFunction.h
@@ -26,7 +26,7 @@
 
 #include <Rcpp/grow.h>
 
-#ifdef RCPP_USING_CXX11
+#if defined(RCPP_USING_CXX11) || defined(HAS_TR1_FUNCTIONAL)
 #include <Rcpp/InternalFunctionWithStdFunction.h>
 #endif
 
@@ -37,7 +37,7 @@ namespace Rcpp{
 
         RCPP_GENERATE_CTOR_ASSIGN(InternalFunction_Impl)
 
-#ifdef RCPP_USING_CXX11
+#if defined(RCPP_USING_CXX11) || defined(HAS_TR1_FUNCTIONAL)
         template <typename RESULT_TYPE, typename... Args>
         InternalFunction_Impl(const std::function<RESULT_TYPE(Args...)> &fun) {
             set(
diff --git a/inst/include/Rcpp/InternalFunctionWithStdFunction.h b/inst/include/Rcpp/InternalFunctionWithStdFunction.h
index 966da22..429bc19 100644
--- a/inst/include/Rcpp/InternalFunctionWithStdFunction.h
+++ b/inst/include/Rcpp/InternalFunctionWithStdFunction.h
@@ -23,7 +23,12 @@
 #ifndef Rcpp_InternalFunctionWithStdFunction_h
 #define Rcpp_InternalFunctionWithStdFunction_h
 
-#include <functional>
+#if defined(HAS_TR1_FUNCTIONAL)
+    #include <tr1/functional>
+    namespace std { using namespace tr1; };
+#else
+    #include <functional>
+#endif
 
 namespace Rcpp {
 
diff --git a/inst/include/Rcpp/platform/compiler.h b/inst/include/Rcpp/platform/compiler.h
index 3a00003..4e512df 100644
--- a/inst/include/Rcpp/platform/compiler.h
+++ b/inst/include/Rcpp/platform/compiler.h
@@ -76,7 +76,10 @@
     #endif
 #elif defined(__clang__)
     #if __cplusplus >= 201103L
-        #define RCPP_USING_CXX11
+        // This compile guard excludes considering Apple macOS 10.7's clang-using-libstdc++ from being considered a conformant C++11 implementation
+        #if !defined(__APPLE__) || (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090)
+            #define RCPP_USING_CXX11
+        #endif
         #if __has_feature(cxx_variadic_templates)
             // #define HAS_VARIADIC_TEMPLATES
         #endif
@@ -136,6 +139,12 @@
         #if __has_include(<tr1/unordered_set>)
             #define HAS_TR1_UNORDERED_SET
         #endif
+        #if __has_include(<tr1/type_traits>)
+            #define HAS_TR1_TYPE_TRAITS
+        #endif
+        #if __has_include(<tr1/functional>)
+            #define HAS_TR1_FUNCTIONAL
+        #endif
     #endif
 #endif
 
diff --git a/inst/include/Rcpp/sugar/functions/sapply.h b/inst/include/Rcpp/sugar/functions/sapply.h
index 2ade406..1d0ec69 100644
--- a/inst/include/Rcpp/sugar/functions/sapply.h
+++ b/inst/include/Rcpp/sugar/functions/sapply.h
@@ -23,7 +23,12 @@
 #define Rcpp__sugar__sapply_h
 
 #if defined(RCPP_USING_CXX0X_OR_LATER)
-	#include <type_traits> // ::std::result_of
+    #if defined HAS_TR1_TYPE_TRAITS
+        #include <tr1/type_traits>
+        namespace std { using namespace tr1; };
+    #else
+        #include <type_traits> // ::std::result_of
+    #endif
 #endif
 
 namespace Rcpp{
@@ -33,7 +38,7 @@ template <typename Function, typename SugarExpression>
 struct sapply_application_result_of
 {
 #if defined(RCPP_USING_CXX0X_OR_LATER)
-	typedef typename ::std::result_of<Function(typename SugarExpression::stored_type)>::type type;
+        typedef typename ::std::result_of<Function(typename SugarExpression::stored_type)>::type type;
 #else
 	typedef typename ::Rcpp::traits::result_of<Function>::type type;
 #endif
-- 
2.9.3

