Skip to content

Commit 67b60cc

Browse files
committed
Resolve #213: Cannot sapply lambda functions
Explanation: #213 (comment)
1 parent 7d78cb8 commit 67b60cc

File tree

1 file changed

+31
-8
lines changed
  • inst/include/Rcpp/sugar/functions

1 file changed

+31
-8
lines changed

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

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

25+
#if (__cplusplus >= 201103L) || defined(__GXX_EXPERIMENTAL_CXX0X__)
26+
#define RCPP_USE_CXX11 1
27+
#else
28+
#define RCPP_USE_CXX11 0
29+
#endif
30+
31+
#if RCPP_USE_CXX11
32+
#include <type_traits>
33+
#endif
34+
2535
namespace Rcpp{
2636
namespace sugar{
2737

38+
template <typename Function, typename SugarExpression>
39+
struct sapply_application_result_of
40+
{
41+
#if RCPP_USE_CXX11
42+
typedef typename ::std::result_of<Function(typename SugarExpression::stored_type)>::type type;
43+
#else
44+
typedef typename ::Rcpp::traits::result_of<Function>::type type;
45+
#endif
46+
} ;
47+
48+
// template <typename Function, typename SugarExpression>
49+
// using sapply_application_result_of_t = typename sapply_application_result_of<Function, SugarExpression>::type;
50+
2851
template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
2952
class Sapply : public VectorBase<
3053
Rcpp::traits::r_sexptype_traits<
31-
typename ::Rcpp::traits::result_of<Function>::type
54+
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
3255
>::rtype ,
3356
true ,
3457
Sapply<RTYPE,NA,T,Function,NO_CONVERSION>
3558
> {
3659
public:
37-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
60+
typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
3861
const static int RESULT_R_TYPE =
3962
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
4063

@@ -65,13 +88,13 @@ class Sapply : public VectorBase<
6588
template <int RTYPE, bool NA, typename T, typename Function>
6689
class Sapply<RTYPE,NA,T,Function,true> : public VectorBase<
6790
Rcpp::traits::r_sexptype_traits<
68-
typename ::Rcpp::traits::result_of<Function>::type
91+
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
6992
>::rtype ,
7093
true ,
7194
Sapply<RTYPE,NA,T,Function,true>
7295
> {
7396
public:
74-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
97+
typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
7598
const static int RESULT_R_TYPE =
7699
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
77100

@@ -102,15 +125,15 @@ template <int RTYPE, bool NA, typename T, typename Function >
102125
inline sugar::Sapply<
103126
RTYPE,NA,T,Function,
104127
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
128+
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
129+
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type >::rtype >::type
107130
>::value
108131
>
109132
sapply( const Rcpp::VectorBase<RTYPE,NA,T>& t, Function fun ){
110133
return sugar::Sapply<RTYPE,NA,T,Function,
111134
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
135+
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
136+
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type >::rtype >::type
114137
>::value >( t, fun ) ;
115138
}
116139

0 commit comments

Comments
 (0)