Skip to content

Commit d9e8596

Browse files
committed
generalize sapply solution to get result_of functions
1 parent 30edbf5 commit d9e8596

File tree

2 files changed

+23
-45
lines changed

2 files changed

+23
-45
lines changed

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

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
// sapply.h: Rcpp R/C++ interface class library -- sapply
33
//
4-
// Copyright (C) 2010 - 2023 Dirk Eddelbuettel and Romain Francois
4+
// Copyright (C) 2010 - 2024 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
56
//
67
// This file is part of Rcpp.
78
//
@@ -27,36 +28,16 @@
2728
namespace Rcpp{
2829
namespace sugar{
2930

30-
template <typename Function, typename SugarExpression>
31-
struct sapply_application_result_of
32-
{
33-
#if __cplusplus >= 201103L
34-
#if __cplusplus < 201703L
35-
// deprecated by C++17, removed by C++2020, see https://en.cppreference.com/w/cpp/types/result_of
36-
typedef typename ::std::result_of<Function(typename SugarExpression::stored_type)>::type type;
37-
#else
38-
// since C++17, see https://en.cppreference.com/w/cpp/types/result_of
39-
typedef typename ::std::invoke_result<Function, typename SugarExpression::stored_type>::type type;
40-
#endif
41-
#else
42-
// TODO this else branch can likely go
43-
typedef typename ::Rcpp::traits::result_of<Function>::type type;
44-
#endif
45-
};
46-
47-
// template <typename Function, typename SugarExpression>
48-
// using sapply_application_result_of_t = typename sapply_application_result_of<Function, SugarExpression>::type;
49-
5031
template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
5132
class Sapply : public VectorBase<
5233
Rcpp::traits::r_sexptype_traits<
53-
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
34+
typename ::Rcpp::traits::result_of<Function, T>::type
5435
>::rtype ,
5536
true ,
5637
Sapply<RTYPE,NA,T,Function,NO_CONVERSION>
5738
> {
5839
public:
59-
typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
40+
typedef typename ::Rcpp::traits::result_of<Function, T>::type result_type ;
6041
const static int RESULT_R_TYPE =
6142
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
6243

@@ -87,13 +68,13 @@ class Sapply : public VectorBase<
8768
template <int RTYPE, bool NA, typename T, typename Function>
8869
class Sapply<RTYPE,NA,T,Function,true> : public VectorBase<
8970
Rcpp::traits::r_sexptype_traits<
90-
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
71+
typename ::Rcpp::traits::result_of<Function, T>::type
9172
>::rtype ,
9273
true ,
9374
Sapply<RTYPE,NA,T,Function,true>
9475
> {
9576
public:
96-
typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
77+
typedef typename ::Rcpp::traits::result_of<Function, T>::type result_type ;
9778
const static int RESULT_R_TYPE =
9879
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
9980

@@ -124,15 +105,15 @@ template <int RTYPE, bool NA, typename T, typename Function >
124105
inline sugar::Sapply<
125106
RTYPE,NA,T,Function,
126107
traits::same_type<
127-
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
128-
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type >::rtype >::type
108+
typename ::Rcpp::traits::result_of<Function, T>::type ,
109+
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of<Function, T>::type >::rtype >::type
129110
>::value
130111
>
131112
sapply( const Rcpp::VectorBase<RTYPE,NA,T>& t, Function fun ){
132113
return sugar::Sapply<RTYPE,NA,T,Function,
133114
traits::same_type<
134-
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
135-
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type >::rtype >::type
115+
typename ::Rcpp::traits::result_of<Function, T>::type ,
116+
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of<Function, T>::type >::rtype >::type
136117
>::value >( t, fun ) ;
137118
}
138119

inst/include/Rcpp/traits/result_of.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
//
44
// result_of.h: Rcpp R/C++ interface class library -- traits to help wrap
55
//
6-
// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2010 - 2024 Dirk Eddelbuettel and Romain Francois
7+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois, Iñaki Ucar
78
//
89
// This file is part of Rcpp.
910
//
@@ -26,24 +27,20 @@
2627
namespace Rcpp{
2728
namespace traits{
2829

29-
template <typename T>
30+
template <typename T, typename... Args>
3031
struct result_of{
31-
typedef typename T::result_type type ;
32-
} ;
33-
34-
template <typename RESULT_TYPE, typename INPUT_TYPE>
35-
struct result_of< RESULT_TYPE (*)(INPUT_TYPE) >{
36-
typedef RESULT_TYPE type ;
37-
} ;
38-
39-
template <typename RESULT_TYPE, typename U1, typename U2>
40-
struct result_of< RESULT_TYPE (*)(U1, U2) >{
41-
typedef RESULT_TYPE type ;
32+
#if __cplusplus < 201703L
33+
// deprecated by C++17, removed by C++2020, see https://en.cppreference.com/w/cpp/types/result_of
34+
typedef typename ::std::result_of<T(typename Args::stored_type...)>::type type;
35+
#else
36+
// since C++17, see https://en.cppreference.com/w/cpp/types/result_of
37+
typedef typename ::std::invoke_result<T, typename Args::stored_type...>::type type;
38+
#endif
4239
} ;
4340

44-
template <typename RESULT_TYPE, typename U1, typename U2, typename U3>
45-
struct result_of< RESULT_TYPE (*)(U1, U2, U3) >{
46-
typedef RESULT_TYPE type ;
41+
template <typename T>
42+
struct result_of<T>{
43+
typename T::result_type type ;
4744
} ;
4845

4946
}

0 commit comments

Comments
 (0)