Skip to content

Commit a959c01

Browse files
committed
additional unary_function conversion in test file and vignettes
1 parent c4b1674 commit a959c01

File tree

6 files changed

+62
-48
lines changed

6 files changed

+62
-48
lines changed

ChangeLog

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
2022-03-09 Dirk Eddelbuettel <[email protected]>
1+
2022-03-10 Dirk Eddelbuettel <[email protected]>
22

33
* DESCRIPTION (Version, Date): Roll minor version
44
* inst/include/Rcpp/config.h: Idem
55

6+
* inst/tinytest/cpp/sugar.cpp: Also conditionally replace
7+
std::unary_function with std::function
8+
* vignettes/rmd/Rcpp-sugar.Rmd: Replace std::unary_function example
9+
with std::function (implicitly requiring C++ in vignette example)
10+
* vignettes/rmd/Rcpp-FAQ.Rmd: Idem
11+
12+
2022-03-09 Dirk Eddelbuettel <[email protected]>
13+
614
* inst/include/Rcpp/Language.h: To quieten compilations, replace
715
std::(unary|binary)_function with std::function for C++11 or later
816
* inst/include/Rcpp/StringTransformer.he: Idem

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: Rcpp
22
Title: Seamless R and C++ Integration
33
Version: 1.0.8.2
4-
Date: 2022-03-09
4+
Date: 2022-03-10
55
Author: Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey, Qiang Kou,
66
Nathan Russell, Inaki Ucar, Douglas Bates and John Chambers
77
Maintainer: Dirk Eddelbuettel <[email protected]>

inst/NEWS.Rd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
\newcommand{\ghpr}{\href{https://github.com/RcppCore/Rcpp/pull/#1}{##1}}
44
\newcommand{\ghit}{\href{https://github.com/RcppCore/Rcpp/issues/#1}{##1}}
55

6-
\section{Changes in Rcpp hotfix release version 1.0.8.2 (2022-03-09)}{
6+
\section{Changes in Rcpp hotfix release version 1.0.8.2 (2022-03-10)}{
77
\itemize{
88
\item Changes in Rcpp API:
99
\itemize{
1010
\item Accomodate C++98 compilation by adjusting attributes.cpp (Dirk in
1111
\ghpr{1193} fixing \ghit{1192})
12-
\item Accomodate newest compilers by replacing deprecated
12+
\item Accomodate newest compilers replacing deprecated
1313
\code{std::unary_function} and \code{std::binary_function} with
14-
\code{std::function} (Dirk fixing \ghit{1201} and CRAN request)
14+
\code{std::function} (Dirk in \ghpr{1202} fixing \ghit{1201} and
15+
CRAN request)
1516
}
1617
\item Changes in Rcpp Documentation:
1718
\itemize{

inst/tinytest/cpp/sugar.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2-
//
1+
32
// sugar.cpp: Rcpp R/C++ interface class library -- sugar unit tests
43
//
5-
// Copyright (C) 2012 - 2015 Dirk Eddelbuettel and Romain Francois
4+
// Copyright (C) 2012 - 2022 Dirk Eddelbuettel and Romain Francois
65
//
76
// This file is part of Rcpp.
87
//
@@ -23,7 +22,12 @@
2322
using namespace Rcpp ;
2423

2524
template <typename T>
25+
26+
#if __cplusplus < 201103L
2627
class square : public std::unary_function<T,T> {
28+
#else
29+
class square : public std::function<T(T)> {
30+
#endif
2731
public:
2832
T operator()( T t) const { return t*t ; }
2933
} ;

vignettes/rmd/Rcpp-FAQ.Rmd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,13 +642,14 @@ what we show below.
642642

643643

644644
### Using inline with Templated Code
645+
645646
Most certainly, consider this simple example of a templated class
646647
which squares its argument:
647648

648649
```{r}
649650
inc <- 'template <typename T>
650651
class square :
651-
public std::unary_function<T,T> {
652+
public std::function<T(T)> {
652653
public:
653654
T operator()( T t) const {
654655
return t*t;
@@ -684,7 +685,7 @@ will even run the R part at the end.
684685
#include <Rcpp.h>
685686

686687
template <typename T> class square :
687-
public std::unary_function<T,T> {
688+
public std::function<T(T)> {
688689
public:
689690
T operator()( T t) const {
690691
return t*t ;

vignettes/rmd/Rcpp-sugar.Rmd

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ high-level \proglang{R} syntax in \proglang{C++}. Hence, with \sugar, the
137137
\proglang{C++} version of `foo` now becomes:
138138

139139
```cpp
140-
Rcpp::NumericVector foo(Rcpp::NumericVector x,
140+
Rcpp::NumericVector foo(Rcpp::NumericVector x,
141141
Rcpp::NumericVector y) {
142142
return ifelse(x < y, x * x, -(y * y));
143143
}
@@ -308,7 +308,7 @@ length. Each element of the result expression evaluates to `TRUE` if
308308
the corresponding input is a missing value, or `FALSE` otherwise.
309309

310310
```cpp
311-
IntegerVector x =
311+
IntegerVector x =
312312
IntegerVector::create(0, 1, NA_INTEGER, 3);
313313

314314
is_na(x)
@@ -322,7 +322,7 @@ Given a sugar expression of any type, `seq_along` creates an
322322
integer sugar expression whose values go from 1 to the size of the input.
323323
324324
```cpp
325-
IntegerVector x =
325+
IntegerVector x =
326326
IntegerVector::create( 0, 1, NA_INTEGER, 3 );
327327
328328
IntegerVector y = seq_along(x);
@@ -337,7 +337,7 @@ second expression, since the abstract syntax tree is built at compile time.
337337

338338
### seq_len
339339

340-
`seq_len` creates an integer sugar expression whose
340+
`seq_len` creates an integer sugar expression whose
341341
\ith\ element expands to `i`. `seq_len` is particularly useful in
342342
conjunction with `sapply` and `lapply`.
343343

@@ -411,7 +411,7 @@ called `result_type`
411411
412412
```cpp
413413
template <typename T>
414-
struct square : std::unary_function<T, T> {
414+
struct square : std::function<T(T)> {
415415
T operator()(const T& x){
416416
return x * x;
417417
}
@@ -491,9 +491,9 @@ beta, binom, cauchy, chisq, exp, f, gamma, geom, hyper, lnorm, logis, nbeta,
491491
nbinom, nbinom_mu, nchisq, nf, norm, nt, pois, t, unif, and weibull.
492492

493493
Note that the parameterization used in these sugar functions may differ between
494-
the top-level functions exposed in an \proglang{R} session. For example,
494+
the top-level functions exposed in an \proglang{R} session. For example,
495495
the internal \code{rexp} is parameterized by \code{scale},
496-
whereas the R-level \code{stats::rexp} is parameterized by \code{rate}.
496+
whereas the R-level \code{stats::rexp} is parameterized by \code{rate}.
497497
Consult \href{http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Distribution-functions}{Distribution Functions}
498498
for more details on the parameterization used for these sugar functions.
499499

@@ -568,13 +568,13 @@ is given here:
568568
template <int RTYPE, bool na, typename VECTOR>
569569
class VectorBase {
570570
public:
571-
struct r_type :
571+
struct r_type :
572572
traits::integral_constant<int,RTYPE>{};
573573
struct can_have_na :
574574
traits::integral_constant<bool,na>{};
575-
576-
typedef typename
577-
traits::storage_type<RTYPE>::type
575+
576+
typedef typename
577+
traits::storage_type<RTYPE>::type
578578
stored_type;
579579
580580
VECTOR& get_ref(){
@@ -586,18 +586,18 @@ public:
586586
this)->operator[](i);
587587
}
588588
589-
inline int size() const {
589+
inline int size() const {
590590
return static_cast<const VECTOR*>(
591-
this)->size();
591+
this)->size();
592592
}
593593
594594
/* definition ommited here */
595595
class iterator;
596596
597-
inline iterator begin() const {
598-
return iterator(*this, 0);
597+
inline iterator begin() const {
598+
return iterator(*this, 0);
599599
}
600-
inline iterator end() const {
600+
inline iterator end() const {
601601
return iterator(*this, size());
602602
}
603603
}
@@ -633,27 +633,27 @@ the template class `Rcpp::sugar::Sapply` is given below:
633633
template <int RTYPE, bool NA,
634634
typename T, typename Function>
635635
class Sapply : public VectorBase<
636-
Rcpp::traits::r_sexptype_traits< typename
636+
Rcpp::traits::r_sexptype_traits< typename
637637
::Rcpp::traits::result_of<Function>::type
638638
>::rtype,
639639
true,
640640
Sapply<RTYPE, NA, T, Function>
641641
> {
642642
public:
643-
typedef typename
643+
typedef typename
644644
::Rcpp::traits::result_of<Function>::type;
645-
645+
646646
const static int RESULT_R_TYPE =
647647
Rcpp::traits::r_sexptype_traits<
648648
result_type>::rtype;
649-
649+
650650
typedef Rcpp::VectorBase<RTYPE,NA,T> VEC;
651-
652-
typedef typename
651+
652+
typedef typename
653653
Rcpp::traits::r_vector_element_converter<
654654
RESULT_R_TYPE>::type
655655
converter_type;
656-
656+
657657
typedef typename Rcpp::traits::storage_type<
658658
RESULT_R_TYPE>::type STORAGE;
659659

@@ -663,9 +663,9 @@ public:
663663
inline STORAGE operator[]( int i ) const {
664664
return converter_type::get(fun(vec[i]));
665665
}
666-
667-
inline int size() const {
668-
return vec.size();
666+
667+
inline int size() const {
668+
return vec.size();
669669
}
670670

671671
private:
@@ -675,13 +675,13 @@ private:
675675

676676
// sugar
677677

678-
template <int RTYPE, bool _NA_,
678+
template <int RTYPE, bool _NA_,
679679
typename T, typename Function >
680680
inline sugar::Sapply<RTYPE, _NA_, T, Function>
681681
sapply(const Rcpp::VectorBase<RTYPE,_NA_,T>& t,
682682
Function fun) {
683-
684-
return
683+
684+
return
685685
sugar::Sapply<RTYPE,_NA_,T,Function>(t, fun);
686686
}
687687
```
@@ -703,8 +703,8 @@ template class queries the template argument via the `Rcpp::traits::result_of`
703703
template.
704704
705705
```cpp
706-
typedef typename
707-
::Rcpp::traits::result_of<Function>::type
706+
typedef typename
707+
::Rcpp::traits::result_of<Function>::type
708708
result_type;
709709
```
710710

@@ -717,7 +717,7 @@ struct result_of{
717717
typedef typename T::result_type type;
718718
};
719719
720-
template <typename RESULT_TYPE,
720+
template <typename RESULT_TYPE,
721721
typename INPUT_TYPE>
722722
struct result_of<RESULT_TYPE (*)(INPUT_TYPE)> {
723723
typedef RESULT_TYPE type;
@@ -762,7 +762,7 @@ of a `REALSXP` expression is `double`.
762762

763763
```cpp
764764
typedef typename
765-
Rcpp::traits::storage_type<RESULT_R_TYPE>::type
765+
Rcpp::traits::storage_type<RESULT_R_TYPE>::type
766766
STORAGE;
767767
```
768768

@@ -785,7 +785,7 @@ template <int RTYPE, bool NA,
785785
typename T, typename Function>
786786
class Sapply : public VectorBase<
787787
Rcpp::traits::r_sexptype_traits<
788-
typename
788+
typename
789789
::Rcpp::traits::result_of<Function>::type
790790
>::rtype,
791791
true,
@@ -799,13 +799,13 @@ is the manifestation of the _CRTP_.
799799
800800
### Constructor
801801
802-
802+
803803
The constructor of the `Sapply` class template is straightforward, it
804804
simply consists of holding the reference to the input expression and the
805805
function.
806806
807807
```cpp
808-
Sapply(const VEC& vec_, Function fun_):
808+
Sapply(const VEC& vec_, Function fun_):
809809
vec(vec_), fun(fun_){}
810810
811811
private:
@@ -825,8 +825,8 @@ and the converter. Both these methods are inline to maximize performance:
825825
inline STORAGE operator[](int i) const {
826826
return converter_type::get(fun(vec[i]));
827827
}
828-
inline int size() const {
829-
return vec.size();
828+
inline int size() const {
829+
return vec.size();
830830
}
831831
```
832832

0 commit comments

Comments
 (0)