Skip to content

Commit a1737c1

Browse files
Enable lambdas in all sugar functions (#1373)
* generalize sapply solution to get result_of functions * adapt outer, lapply, mapply * add tests * update Changelog * re-add support for functions without argument specification * fix missing typedef * Roll micro version and date --------- Co-authored-by: Dirk Eddelbuettel <[email protected]>
1 parent 30df687 commit a1737c1

File tree

11 files changed

+179
-70
lines changed

11 files changed

+179
-70
lines changed

Diff for: ChangeLog

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
2025-03-31 Dirk Eddelbuettel <[email protected]>
2+
3+
* DESCRIPTION (Version, Date): Roll micro version and date
4+
* inst/include/Rcpp/config.h: Idem
5+
6+
2025-03-29 Iñaki Ucar <[email protected]>
7+
8+
* inst/include/Rcpp/traits/result_of.h: Reimplement traits::result_of using
9+
std::result_of (up to C++14) or std::invoke_result (C++17 or later), which
10+
supports previous use cases and enables lambdas
11+
* inst/include/Rcpp/sugar/functions/sapply.h: Use new result_of interface
12+
* inst/include/Rcpp/sugar/functions/lapply.h: Idem
13+
* inst/include/Rcpp/sugar/functions/mapply/mapply_2.h: Idem
14+
* inst/include/Rcpp/sugar/functions/mapply/mapply_3.h: Idem
15+
* inst/include/Rcpp/sugar/matrix/outer.h: Idem
16+
* inst/tinytest/cpp/sugar.cpp: New tests for previous sugar functions
17+
* inst/tinytest/test_sugar.R: Idem
18+
119
2025-03-26 Dirk Eddelbuettel <[email protected]>
220

321
* DESCRIPTION (Version, Date): Roll micro version and date

Diff for: DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: Rcpp
22
Title: Seamless R and C++ Integration
3-
Version: 1.0.14.11
4-
Date: 2025-03-26
3+
Version: 1.0.14.12
4+
Date: 2025-03-31
55
Authors@R: c(person("Dirk", "Eddelbuettel", role = c("aut", "cre"), email = "[email protected]",
66
comment = c(ORCID = "0000-0001-6419-907X")),
77
person("Romain", "Francois", role = "aut",

Diff for: inst/include/Rcpp/config.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#define RCPP_VERSION_STRING "1.0.14"
3131

3232
// the current source snapshot (using four components, if a fifth is used in DESCRIPTION we ignore it)
33-
#define RCPP_DEV_VERSION RcppDevVersion(1,0,14,11)
34-
#define RCPP_DEV_VERSION_STRING "1.0.14.11"
33+
#define RCPP_DEV_VERSION RcppDevVersion(1,0,14,12)
34+
#define RCPP_DEV_VERSION_STRING "1.0.14.12"
3535

3636
#endif

Diff for: inst/include/Rcpp/sugar/functions/lapply.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// lapply.h: Rcpp R/C++ interface class library -- lapply
44
//
5-
// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2010 - 2024 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
67
//
78
// This file is part of Rcpp.
89
//
@@ -33,7 +34,7 @@ class Lapply : public VectorBase<
3334
> {
3435
public:
3536
typedef Rcpp::VectorBase<RTYPE,NA,T> VEC ;
36-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
37+
typedef typename ::Rcpp::traits::result_of<Function, T>::type result_type ;
3738

3839
Lapply( const VEC& vec_, Function fun_ ) :
3940
vec(vec_), fun(fun_){}

Diff for: inst/include/Rcpp/sugar/functions/mapply/mapply_2.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// mapply_2.h: Rcpp R/C++ interface class library -- mapply_2
44
//
5-
// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2012 - 2024 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
67
//
78
// This file is part of Rcpp.
89
//
@@ -32,13 +33,13 @@ template <int RTYPE,
3233
>
3334
class Mapply_2 : public VectorBase<
3435
Rcpp::traits::r_sexptype_traits<
35-
typename ::Rcpp::traits::result_of<Function>::type
36+
typename ::Rcpp::traits::result_of<Function, T_1, T_2>::type
3637
>::rtype ,
3738
true ,
3839
Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function>
3940
> {
4041
public:
41-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
42+
typedef typename ::Rcpp::traits::result_of<Function, T_1, T_2>::type result_type ;
4243

4344
Mapply_2( const T_1& vec_1_, const T_2& vec_2_, Function fun_ ) :
4445
vec_1(vec_1_), vec_2(vec_2_), fun(fun_){}
@@ -62,14 +63,14 @@ template <int RTYPE,
6263
class Mapply_2_Vector_Primitive : public
6364
VectorBase<
6465
Rcpp::traits::r_sexptype_traits<
65-
typename ::Rcpp::traits::result_of<Function>::type
66+
typename ::Rcpp::traits::result_of<Function, T_1>::type
6667
>::rtype ,
6768
true ,
6869
Mapply_2_Vector_Primitive<RTYPE,NA_1,T_1,PRIM_2,Function>
6970
>
7071
{
7172
public:
72-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
73+
typedef typename ::Rcpp::traits::result_of<Function, T_1>::type result_type ;
7374

7475
Mapply_2_Vector_Primitive( const T_1& vec_1_, PRIM_2 prim_2_, Function fun_ ) :
7576
vec_1(vec_1_), prim_2(prim_2_), fun(fun_){}
@@ -93,14 +94,14 @@ template <int RTYPE,
9394
class Mapply_2_Primitive_Vector : public
9495
VectorBase<
9596
Rcpp::traits::r_sexptype_traits<
96-
typename ::Rcpp::traits::result_of<Function>::type
97+
typename ::Rcpp::traits::result_of<Function, T_2>::type
9798
>::rtype ,
9899
true ,
99100
Mapply_2_Primitive_Vector<RTYPE,PRIM_1,NA_2,T_2,Function>
100101
>
101102
{
102103
public:
103-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
104+
typedef typename ::Rcpp::traits::result_of<Function, T_2>::type result_type ;
104105

105106
Mapply_2_Primitive_Vector( PRIM_1 prim_1_, const T_2& vec_2_, Function fun_ ) :
106107
prim_1(prim_1_), vec_2(vec_2_), fun(fun_){}

Diff for: inst/include/Rcpp/sugar/functions/mapply/mapply_3.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// mapply_3.h: Rcpp R/C++ interface class library -- mapply_3
44
//
5-
// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2012 - 2024 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
67
//
78
// This file is part of Rcpp.
89
//
@@ -33,13 +34,13 @@ template <
3334
>
3435
class Mapply_3 : public VectorBase<
3536
Rcpp::traits::r_sexptype_traits<
36-
typename ::Rcpp::traits::result_of<Function>::type
37+
typename ::Rcpp::traits::result_of<Function, T_1, T_2, T_3>::type
3738
>::rtype ,
3839
true ,
3940
Mapply_3<RTYPE_1,NA_1,T_1,RTYPE_2,NA_2,T_2,RTYPE_3,NA_3,T_3,Function>
4041
> {
4142
public:
42-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
43+
typedef typename ::Rcpp::traits::result_of<Function, T_1, T_2, T_3>::type result_type ;
4344

4445
typedef Rcpp::VectorBase<RTYPE_1,NA_1,T_1> VEC_1 ;
4546
typedef Rcpp::VectorBase<RTYPE_2,NA_2,T_2> VEC_2 ;

Diff for: inst/include/Rcpp/sugar/functions/sapply.h

+10-32
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
//
@@ -21,42 +22,19 @@
2122
#ifndef Rcpp__sugar__sapply_h
2223
#define Rcpp__sugar__sapply_h
2324

24-
// This used to be conditional on a define and test in compiler.h
25-
#include <type_traits> // ::std::result_of
26-
2725
namespace Rcpp{
2826
namespace sugar{
2927

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-
5028
template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
5129
class Sapply : public VectorBase<
5230
Rcpp::traits::r_sexptype_traits<
53-
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
31+
typename ::Rcpp::traits::result_of<Function, T>::type
5432
>::rtype ,
5533
true ,
5634
Sapply<RTYPE,NA,T,Function,NO_CONVERSION>
5735
> {
5836
public:
59-
typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
37+
typedef typename ::Rcpp::traits::result_of<Function, T>::type result_type ;
6038
const static int RESULT_R_TYPE =
6139
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
6240

@@ -87,13 +65,13 @@ class Sapply : public VectorBase<
8765
template <int RTYPE, bool NA, typename T, typename Function>
8866
class Sapply<RTYPE,NA,T,Function,true> : public VectorBase<
8967
Rcpp::traits::r_sexptype_traits<
90-
typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
68+
typename ::Rcpp::traits::result_of<Function, T>::type
9169
>::rtype ,
9270
true ,
9371
Sapply<RTYPE,NA,T,Function,true>
9472
> {
9573
public:
96-
typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
74+
typedef typename ::Rcpp::traits::result_of<Function, T>::type result_type ;
9775
const static int RESULT_R_TYPE =
9876
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
9977

@@ -124,15 +102,15 @@ template <int RTYPE, bool NA, typename T, typename Function >
124102
inline sugar::Sapply<
125103
RTYPE,NA,T,Function,
126104
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
105+
typename ::Rcpp::traits::result_of<Function, T>::type ,
106+
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of<Function, T>::type >::rtype >::type
129107
>::value
130108
>
131109
sapply( const Rcpp::VectorBase<RTYPE,NA,T>& t, Function fun ){
132110
return sugar::Sapply<RTYPE,NA,T,Function,
133111
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
112+
typename ::Rcpp::traits::result_of<Function, T>::type ,
113+
typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of<Function, T>::type >::rtype >::type
136114
>::value >( t, fun ) ;
137115
}
138116

Diff for: inst/include/Rcpp/sugar/matrix/outer.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// outer.h: Rcpp R/C++ interface class library -- outer
44
//
5-
// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2010 - 2024 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
67
//
78
// This file is part of Rcpp.
89
//
@@ -31,13 +32,13 @@ template <int RTYPE,
3132
typename Function >
3233
class Outer : public MatrixBase<
3334
Rcpp::traits::r_sexptype_traits<
34-
typename ::Rcpp::traits::result_of<Function>::type
35+
typename ::Rcpp::traits::result_of<Function, LHS_T, RHS_T>::type
3536
>::rtype ,
3637
true ,
3738
Outer<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T,Function>
3839
> {
3940
public:
40-
typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
41+
typedef typename ::Rcpp::traits::result_of<Function, LHS_T, RHS_T>::type result_type ;
4142
const static int RESULT_R_TYPE =
4243
Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
4344

Diff for: inst/include/Rcpp/traits/result_of.h

+13-16
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 and 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+
typedef typename T::result_type type ;
4744
} ;
4845

4946
}

0 commit comments

Comments
 (0)