Skip to content

Commit 6e6ce2c

Browse files
committed
Merge pull request #296 from MattPD/patch-1
Resolve #213: Cannot sapply lambda functions
2 parents cb87ebd + 08d1d3e commit 6e6ce2c

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

inst/include/Rcpp/platform/compiler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
#endif
5959
#endif
6060

61+
// Check for the presence of C++0x (or later) support
62+
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L)
63+
#define RCPP_USING_CXX0X_OR_LATER
64+
#endif
65+
6166
// Check C++0x/11 features
6267
#if defined(__INTEL_COMPILER)
6368
#if __cplusplus >= 201103L

inst/include/Rcpp/sugar/functions/sapply.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,36 @@
2222
#ifndef Rcpp__sugar__sapply_h
2323
#define Rcpp__sugar__sapply_h
2424

25+
#if defined(RCPP_USING_CXX0X_OR_LATER)
26+
#include <type_traits> // ::std::result_of
27+
#endif
28+
2529
namespace Rcpp{
2630
namespace sugar{
2731

32+
template <typename Function, typename SugarExpression>
33+
struct sapply_application_result_of
34+
{
35+
#if defined(RCPP_USING_CXX0X_OR_LATER)
36+
typedef typename ::std::result_of<Function(typename SugarExpression::stored_type)>::type type;
37+
#else
38+
typedef typename ::Rcpp::traits::result_of<Function>::type type;
39+
#endif
40+
} ;
41+
42+
// template <typename Function, typename SugarExpression>
43+
// using sapply_application_result_of_t = typename sapply_application_result_of<Function, SugarExpression>::type;
44+
2845
template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
2946
class Sapply : public VectorBase<
3047
Rcpp::traits::r_sexptype_traits<
31-
typename ::Rcpp::traits::result_of<Function>::type
48+
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
3249
>::rtype ,
3350
true ,
3451
Sapply<RTYPE,NA,T,Function,NO_CONVERSION>
3552
> {
3653
public:
37-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
54+
typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
3855
const static int RESULT_R_TYPE =
3956
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
4057

@@ -65,13 +82,13 @@ class Sapply : public VectorBase<
6582
template <int RTYPE, bool NA, typename T, typename Function>
6683
class Sapply<RTYPE,NA,T,Function,true> : public VectorBase<
6784
Rcpp::traits::r_sexptype_traits<
68-
typename ::Rcpp::traits::result_of<Function>::type
85+
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
6986
>::rtype ,
7087
true ,
7188
Sapply<RTYPE,NA,T,Function,true>
7289
> {
7390
public:
74-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
91+
typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
7592
const static int RESULT_R_TYPE =
7693
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
7794

@@ -102,15 +119,15 @@ template <int RTYPE, bool NA, typename T, typename Function >
102119
inline sugar::Sapply<
103120
RTYPE,NA,T,Function,
104121
traits::same_type<
105-
typename ::Rcpp::traits::result_of<Function>::type ,
106-
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of<Function>::type >::rtype >::type
122+
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
123+
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type >::rtype >::type
107124
>::value
108125
>
109126
sapply( const Rcpp::VectorBase<RTYPE,NA,T>& t, Function fun ){
110127
return sugar::Sapply<RTYPE,NA,T,Function,
111128
traits::same_type<
112-
typename ::Rcpp::traits::result_of<Function>::type ,
113-
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of<Function>::type >::rtype >::type
129+
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
130+
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type >::rtype >::type
114131
>::value >( t, fun ) ;
115132
}
116133

0 commit comments

Comments
 (0)