[Rcpp-devel] Patch for using Rcpp with Clang + libc++

Yan Zhou zhouyan at me.com
Sat Dec 1 13:12:46 CET 2012


Hi Dirk,

Rcpp cannot be compiled with clang++ with libc++, even clang++  provides very good standard conforming in both C++98 and C++11 mode, and libc++ provides 100% C++98/11 features. The problems is Rcpp's use of TR1 instead of C++11, and does not perform some compiler checks properly. I made a small patch to the include/RcppCommon.h header, which makes Rcpp works with Clang++ and libc++ in C++11 mode.

To summary the change, when macro __clang__ is defined, the header use clang's __has_include to check if <tr1/unordered_map> and <tr1/unordered_set> is present, if not, it undef HAS_TR1_UNORDERED_MAP etc. In addition, if __has_include<unordered_map> is tested to be true while HAS_TR1_... etc are not true, the C++11 header is included. So  headers like sugar/sets.h can use C++11 header instead of TR1, since they already test __cplusplus >= 201103L.

With this patch, nothing already works will be broken. This patch only affects clang++ with libc++ situation. Using clang++ with libstdc++ the situation will be exactly the same as before.

At the end of the email is the path, it is also attached as a diff file

Best,

Yan Zhou
-------------- next part --------------
A non-text attachment was scrubbed...
Name: RcppCommon.h.diff
Type: application/octet-stream
Size: 1174 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121201/386105d5/attachment.obj>
-------------- next part --------------


--- Rcpp/inst/include/RcppCommon.h	2012-11-23 01:07:34.000000000 +0000
+++ ../Downloads/Rcpp/inst/include/RcppCommon.h	2012-12-01 11:46:48.000000000 +0000
@@ -107,6 +107,20 @@
 //     #endif
 // #endif
 
+#ifdef __clang__
+    #if !__has_include(<tr1/unordered_map>)
+        #undef HAS_TR1
+        #undef HAS_TR1_UNORDERED_MAP
+    #endif
+    #if !__has_include(<tr1/unordered_set>)
+        #undef HAS_TR1
+        #undef HAS_TR1_UNORDERED_SET
+    #endif
+    #if !__has_feature(cxx_variadic_templates)
+        #undef HAS_VARIADIC_TEMPLATES
+    #endif
+#endif
+
 #ifdef __INTEL_COMPILER
     // This is based on an email by Alexey Stukalov who tested 
     // Intel Compiler 12.0 and states that is does support Cxx0x 
@@ -149,6 +163,15 @@
 #include <tr1/unordered_set>
 #endif
 
+#ifdef __clang__
+    #if !defined(HAS_TR1_UNORDERED_MAP) && __has_include(<unordered_map>)
+    #include <unordered_map>
+    #endif
+    #if !defined(HAS_TR1_UNORDERED_SET) && __has_include(<unordered_set>)
+    #include <unordered_set>
+    #endif
+#endif
+
 std::string demangle( const std::string& name) ;
 #define DEMANGLE(__TYPE__) demangle( typeid(__TYPE__).name() ).c_str() 
 



More information about the Rcpp-devel mailing list