-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path050_c++11.Rmd
executable file
·123 lines (78 loc) · 3.12 KB
/
050_c++11.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# C++11
C++ 11 is a standard of C++ newly established in 2011, it introduces new functionalities and notations. Compared with the previous standard, many new features have been added to make C++ even easier for beginners. This document will actively exploit these features of C++11.
**Important: The code examples in this document are written with C++11 enabled.**
## Enabling C++11
To enable `C++11`, add the following description somewhere in your Rcpp code, this is sufficient when you compile your rcpp code with `Rcpp::sourceCpp()`.
```cpp
// [[Rcpp::plugins("cpp11")]]
```
If you want to enable `C++11` in your package, add code below in the `DESCRIPTION` file of your package.
```
SystemRequirements: C++11
```
## Recommended C++11 features
### Initializer list
Initialization of variables using `{}`.
```
// Initialize Vector
// The next three are the same as c (1, 2, 3).
NumericVector v1 = NumericVector::create(1.0, 2.0, 3.0);
NumericVector v2 = {1.0, 2.0, 3.0};
NumericVector v3 {1.0, 2.0, 3.0}; // You can omit "=".
```
### auto
By using the `auto` specifier, the type of a defined variable is inferred by the compiler automatically according to the assigned value.
```
// variable "i" will be int
auto i = 4;
NumericVector v;
// variable "it" will be NumericVector::iterator
auto it = v.begin();
```
### decltype
By using `decltype`, you can declare a variable of the same type as an existing variable.
```
int i;
decltype(i) x; // variable "x" will be int
```
### Range-based for-loop
You can write a `for` statement with the same style as R.
```
IntegerVector v {1,2,3};
int sum=0;
for(auto& x : v) {
sum += x;
}
```
### Lambda expression
You can create a function object by using a lambda expression. A function object is usually created as an unnamed function and passed to the other function.
Lambda expressions are written in the form `[](){}`.
In `[]`, you write a list of local variables you want to use in this function object.
- `[]` do not allow access to all the local variables from the function object.
- `[=]` will copy values of the all local variables to the function object.
- `[&]` enables direct access to all local variables from the function object.
- `[=x, &y]` means that the local variable "x" will be copied to the function object, and the local variable "y" is allowed to be accessed directly from the function object.
In `()`, you write arguments to be passed to this function object.
In `{}`, you describe processes you want.
**Return type of the lambda expression**
The return type of this function object is automatically set to the type of the returned value described in `{}`. If you want to define return type explicitly, write it like `[]()->int{}`.
**Example**
The following example shows how to use a lambda expression. You can find Some types of C++ code can be written in the same style as R.
*R example*
``` R
v <- c(1,2,3,4,5)
A <- 2.0
res <-
sapply(v, function(x){A*x})
```
*Rcpp example*
``` cpp
// [[Rcpp::export]]
NumericVector rcpp_lambda_1(){
NumericVector v = {1,2,3,4,5};
double A = 2.0;
NumericVector res =
sapply(v, [&](double x){return A*x;});
return res;
}
```