File: converting.html

package info (click to toggle)
r-cran-cpp11 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,428 kB
  • sloc: cpp: 9,732; sh: 14; makefile: 2
file content (668 lines) | stat: -rw-r--r-- 23,501 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />

<meta name="viewport" content="width=device-width, initial-scale=1" />



<title>Converting from Rcpp</title>

<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
  var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
  var i, h, a;
  for (i = 0; i < hs.length; i++) {
    h = hs[i];
    if (!/^h[1-6]$/i.test(h.tagName)) continue;  // it should be a header h1-h6
    a = h.attributes;
    while (a.length > 0) h.removeAttribute(a[0].name);
  }
});
</script>

<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>



<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } 
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } 
code span.at { color: #7d9029; } 
code span.bn { color: #40a070; } 
code span.bu { color: #008000; } 
code span.cf { color: #007020; font-weight: bold; } 
code span.ch { color: #4070a0; } 
code span.cn { color: #880000; } 
code span.co { color: #60a0b0; font-style: italic; } 
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } 
code span.do { color: #ba2121; font-style: italic; } 
code span.dt { color: #902000; } 
code span.dv { color: #40a070; } 
code span.er { color: #ff0000; font-weight: bold; } 
code span.ex { } 
code span.fl { color: #40a070; } 
code span.fu { color: #06287e; } 
code span.im { color: #008000; font-weight: bold; } 
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } 
code span.kw { color: #007020; font-weight: bold; } 
code span.op { color: #666666; } 
code span.ot { color: #007020; } 
code span.pp { color: #bc7a00; } 
code span.sc { color: #4070a0; } 
code span.ss { color: #bb6688; } 
code span.st { color: #4070a0; } 
code span.va { color: #19177c; } 
code span.vs { color: #4070a0; } 
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } 
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
  var sheets = document.styleSheets;
  for (var i = 0; i < sheets.length; i++) {
    if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
    try { var rules = sheets[i].cssRules; } catch (e) { continue; }
    var j = 0;
    while (j < rules.length) {
      var rule = rules[j];
      // check if there is a div.sourceCode rule
      if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
        j++;
        continue;
      }
      var style = rule.style.cssText;
      // check if color or background-color is set
      if (rule.style.color === '' && rule.style.backgroundColor === '') {
        j++;
        continue;
      }
      // replace div.sourceCode by a pre.sourceCode rule
      sheets[i].deleteRule(j);
      sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
    }
  }
})();
</script>




<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap; 
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }

code > span.kw { color: #555; font-weight: bold; } 
code > span.dt { color: #902000; } 
code > span.dv { color: #40a070; } 
code > span.bn { color: #d14; } 
code > span.fl { color: #d14; } 
code > span.ch { color: #d14; } 
code > span.st { color: #d14; } 
code > span.co { color: #888888; font-style: italic; } 
code > span.ot { color: #007020; } 
code > span.al { color: #ff0000; font-weight: bold; } 
code > span.fu { color: #900; font-weight: bold; } 
code > span.er { color: #a61717; background-color: #e3d2d2; } 
</style>




</head>

<body>




<h1 class="title toc-ignore">Converting from Rcpp</h1>



<p>In many cases there is no need to convert a package from Rcpp. If the
code is already written and you don’t have a very compelling need to use
cpp11 I would recommend you continue to use Rcpp. However if you
<em>do</em> feel like your project will benefit from using cpp11 this
vignette will provide some guidance and doing the conversion.</p>
<div id="getting-started" class="section level2">
<h2>Getting started</h2>
<ol style="list-style-type: decimal">
<li><p>Add cpp11 by calling <code>usethis::use_cpp11()</code>.</p></li>
<li><p>Start converting function by function.</p>
<p>Converting the code a bit at a time (and regularly running your
tests) is the best way to do the conversion correctly and make progress.
Doing a separate commit after converting each file (or possibly each
function) can make finding any regressions with <a href="https://youtu.be/KKeucpfAuuA">git bisect</a> much easier in the
future.</p>
<ol style="list-style-type: decimal">
<li>Convert <code>#include &lt;Rcpp.h&gt;</code> to
<code>#include &lt;cpp11.hpp&gt;</code>.</li>
<li>Convert all instances of <code>// [[Rcpp::export]]</code> to
<code>[[cpp11::register]]</code>.</li>
<li>Grep for <code>Rcpp::</code> and replace with the equivalent cpp11
function using the cheatsheets below.</li>
</ol></li>
<li><p>Remove Rcpp</p>
<ol style="list-style-type: decimal">
<li>Remove Rcpp from the <code>LinkingTo</code> and <code>Imports</code>
fields.</li>
<li>Remove <code>@importFrom Rcpp sourceCpp</code>.</li>
<li>Delete <code>src/RccpExports.cpp</code> and
<code>R/RcppExports.R</code>.</li>
<li>Delete <code>src/Makevars</code> if it only contains
<code>PKG_CPPFLAGS=-DSTRICT_R_HEADERS</code>.</li>
<li>Clean out old compiled code with
<code>pkgbuild::clean_dll()</code>.</li>
<li>Re-document the package to update the <code>NAMESPACE</code>.</li>
</ol></li>
</ol>
</div>
<div id="cheatsheet" class="section level2">
<h2>Cheatsheet</h2>
<div id="vectors" class="section level3">
<h3>Vectors</h3>
<table>
<colgroup>
<col width="24%" />
<col width="30%" />
<col width="44%" />
</colgroup>
<thead>
<tr class="header">
<th>Rcpp</th>
<th>cpp11 (read-only)</th>
<th>cpp11 (writable)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>NumericVector</td>
<td>doubles</td>
<td>writable::doubles</td>
</tr>
<tr class="even">
<td>NumericMatrix</td>
<td>doubles_matrix&lt;&gt;</td>
<td>writable::doubles_matrix&lt;&gt;</td>
</tr>
<tr class="odd">
<td>IntegerVector</td>
<td>integers</td>
<td>writable::integers</td>
</tr>
<tr class="even">
<td>IntegerMatrix</td>
<td>integers_matrix&lt;&gt;</td>
<td>writable::integers_matrix&lt;&gt;</td>
</tr>
<tr class="odd">
<td>CharacterVector</td>
<td>strings</td>
<td>writable::strings</td>
</tr>
<tr class="even">
<td>RawVector</td>
<td>raws</td>
<td>writable::raws</td>
</tr>
<tr class="odd">
<td>List</td>
<td>list</td>
<td>writable::list</td>
</tr>
<tr class="even">
<td>RObject</td>
<td>sexp</td>
<td></td>
</tr>
</tbody>
</table>
<p>Note that each cpp11 vector class has a read-only and writeable
version. The default classes, e.g. <code>cpp11::doubles</code> are
<em>read-only</em> classes that do not permit modification. If you want
to modify the data you or create a new vector, use the writeable
variant.</p>
<p>Another major difference in Rcpp and cpp11 is how vectors are grown.
Rcpp vectors have a <code>push_back()</code> method, but unlike
<code>std::vector()</code> no additional space is reserved when pushing.
This makes calling <code>push_back()</code> repeatably very expensive,
as the entire vector has to be copied each call. In contrast
<code>cpp11</code> vectors grow efficiently, reserving extra space. See
<a href="https://cpp11.r-lib.org/articles/motivations.html#growing-vectors" class="uri">https://cpp11.r-lib.org/articles/motivations.html#growing-vectors</a>
for more details.</p>
<p>Rcpp also allows very flexible implicit conversions, e.g. if you pass
a <code>REALSXP</code> to a function that takes a
<code>Rcpp::IntegerVector()</code> it is implicitly converted to a
<code>INTSXP</code>. These conversions are nice for usability, but
require (implicit) duplication of the data, with the associated runtime
costs. cpp11 throws an error in these cases. If you want the implicit
coercions you can add a call to <code>as.integer()</code> or
<code>as.double()</code> as appropriate from R when you call the
function.</p>
</div>
<div id="other-objects" class="section level3">
<h3>Other objects</h3>
<table>
<thead>
<tr class="header">
<th>Rcpp</th>
<th>cpp11</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>XPtr</td>
<td>external_pointer</td>
</tr>
<tr class="even">
<td>Environment</td>
<td>environment</td>
</tr>
<tr class="odd">
<td>Function</td>
<td>function</td>
</tr>
<tr class="even">
<td>Environment (namespace)</td>
<td>package</td>
</tr>
</tbody>
</table>
</div>
<div id="functions" class="section level3">
<h3>Functions</h3>
<table>
<thead>
<tr class="header">
<th>Rcpp</th>
<th>cpp11</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>wrap()</code></td>
<td><code>as_sexp()</code></td>
</tr>
<tr class="even">
<td><code>as()</code></td>
<td><code>as_cpp()</code></td>
</tr>
<tr class="odd">
<td><code>stop()</code></td>
<td><code>stop()</code></td>
</tr>
<tr class="even">
<td><code>checkUserInterrupt()</code></td>
<td><code>check_user_interrupt()</code></td>
</tr>
<tr class="odd">
<td><code>CharacterVector::create(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)</code></td>
<td><code>{&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}</code></td>
</tr>
</tbody>
</table>
<p>Note that <code>cpp11::stop()</code> and
<code>cpp11::warning()</code> are thin wrappers around
<code>Rf_stop()</code> and <code>Rf_warning()</code>. These are simple C
functions with a <code>printf()</code> API, so they do not understand
C++ objects like <code>std::string</code>. Therefore you need to call
<code>obj.c_str()</code> when passing string data to them.</p>
</div>
<div id="r-functions" class="section level3">
<h3>R functions</h3>
<p>Calling R functions from C++ is similar to using Rcpp.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="co">// Rcpp -----------------------------------------------</span></span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a>Rcpp<span class="op">::</span>Function as_tibble<span class="op">(</span><span class="st">&quot;as_tibble&quot;</span><span class="op">,</span> Rcpp<span class="op">::</span>Environment<span class="op">::</span>namespace_env<span class="op">(</span><span class="st">&quot;tibble&quot;</span><span class="op">));</span></span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a>as_tibble<span class="op">(</span>x<span class="op">,</span> Rcpp<span class="op">::</span>Named<span class="op">(</span><span class="st">&quot;.rows&quot;</span><span class="op">,</span> num_rows<span class="op">),</span> Rcpp<span class="op">::</span>Named<span class="op">(</span><span class="st">&quot;.name_repair&quot;</span><span class="op">,</span> name_repair<span class="op">));</span></span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="co">// cpp11 -----------------------------------------------</span></span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="kw">using</span> <span class="kw">namespace</span> cpp11<span class="op">::</span>literals<span class="op">;</span> <span class="co">// so we can use &quot;&quot;_nm syntax</span></span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="kw">auto</span> as_tibble <span class="op">=</span> cpp11<span class="op">::</span>package<span class="op">(</span><span class="st">&quot;tibble&quot;</span><span class="op">)[</span><span class="st">&quot;as_tibble&quot;</span><span class="op">];</span></span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a>as_tibble<span class="op">(</span>x<span class="op">,</span> <span class="st">&quot;.rows&quot;_nm</span> <span class="op">=</span> num_rows<span class="op">,</span> <span class="st">&quot;.name_repair&quot;_nm</span> <span class="op">=</span> name_repair<span class="op">);</span></span></code></pre></div>
</div>
<div id="unsupported-rcpp-features" class="section level3">
<h3>Unsupported Rcpp features</h3>
<ul>
<li>None of <a href="https://CRAN.R-project.org/package=Rcpp/vignettes/Rcpp-modules.pdf">Modules</a></li>
<li>None of <a href="https://CRAN.R-project.org/package=Rcpp/vignettes/Rcpp-sugar.pdf">Sugar</a></li>
<li>Some parts of <a href="https://CRAN.R-project.org/package=Rcpp/vignettes/Rcpp-attributes.pdf">Attributes</a>
<ul>
<li>No dependencies</li>
<li>No random number generator restoration</li>
<li>No support for roxygen2 comments</li>
<li>No interfaces</li>
</ul></li>
</ul>
</div>
<div id="rngs" class="section level3">
<h3>RNGs</h3>
<p>Rcpp includes calls to <code>GetRNGstate()</code> and
<code>PutRNGstate()</code> around the wrapped function. This ensures
that if any C++ code calls the R API functions <code>unif_rand()</code>,
<code>norm_rand()</code>, <code>exp_rand()</code>, or
<code>R_unif_index()</code> the random seed state is set accordingly.
cpp11 does <em>not</em> do this, so you must include the calls to
<code>GetRNGstate()</code> and <code>PutRNGstate()</code>
<em>yourself</em> if you use any of those functions in your C++ code.
See <a href="https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Random-numbers">R-exts
6.3 - Random number generation</a> for details on these functions.</p>
<p>One convenient way to do safely is to use a simple class:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="kw">class</span> local_rng <span class="op">{</span></span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="kw">public</span><span class="op">:</span></span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a>  local_rng<span class="op">()</span> <span class="op">{</span></span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a>    GetRNGstate<span class="op">();</span></span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a>  <span class="op">}</span></span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a></span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a>  <span class="op">~</span>local_rng<span class="op">(){</span></span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a>    PutRNGstate<span class="op">();</span></span>
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a>  <span class="op">}</span></span>
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="op">};</span></span>
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a></span>
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a><span class="dt">void</span> foo<span class="op">()</span> <span class="op">{</span></span>
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a>  local_rng rng_state<span class="op">;</span></span>
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a>  <span class="co">/* my code using the RNG */</span></span>
<span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
</div>
</div>
<div id="common-issues-when-converting" class="section level2">
<h2>Common issues when converting</h2>
<div id="stl-includes" class="section level3">
<h3>STL includes</h3>
<p>Rcpp.h includes a number of STL headers automatically, notably
<code>&lt;string&gt;</code> and <code>&lt;vector&gt;</code>, however the
cpp11 headers generally do not. If you have errors like</p>
<pre><code>error: no type named &#39;string&#39; in namespace &#39;std&#39;</code></pre>
<p>You will need to include the appropriate STL header, in this case
<code>&lt;string&gt;</code>.</p>
</div>
<div id="strict-headers" class="section level3">
<h3>Strict headers</h3>
<p>If you see something like this:</p>
<pre><code> In file included from file.cpp:1:
 In file included from path/cpp11/include/cpp11.hpp:3:
 path/cpp11/include/cpp11/R.hpp:12:9: warning: &#39;STRICT_R_HEADERS&#39; macro redefined [-Wmacro-redefined]
 #define STRICT_R_HEADERS</code></pre>
<p>Make sure to remove <code>PKG_CPPFLAGS=-DSTRICT_R_HEADERS</code> from
<code>src/Makevars</code>.</p>
</div>
<div id="r-api-includes" class="section level3">
<h3>R API includes</h3>
<p>cpp11 conflicts with macros declared by some R headers unless the
macros <code>R_NO_REMAP</code> and <code>STRICT_R_HEADERS</code> are
defined. If you include <code>cpp11.hpp</code> (or, at a minimum,
<code>cpp11/R.hpp</code>) before any R headers these macros will be
defined appropriately, otherwise you may see errors like</p>
<blockquote>
<p>R headers were included before cpp11 headers and at least one of
R_NO_REMAP or STRICT_R_HEADERS was not defined.</p>
</blockquote>
<p>Which indicate that you must either change your include order or add
preprocessor definitions for <code>R_NO_REMAP</code> and
<code>STRICT_R_HEADERS</code>. Note that transitive includes of R
headers (for example, those included by <code>Rcpp.h</code>) can also
introduce the conflicting macros.</p>
</div>
<div id="type-aliases" class="section level3">
<h3>Type aliases</h3>
<p>If you use typedefs for cpp11 types or define custom types you will
need to define them in a <code>pkgname_types.hpp</code> file so that
<code>cpp_register()</code> can include it in the generated code.</p>
</div>
<div id="logical-vector-construction" class="section level3">
<h3>Logical vector construction</h3>
<p>If you are constructing a length 1 logical vector you may need to
explicitly use a <code>r_bool()</code> object in the initializer list
rather than <code>TRUE</code>, <code>FALSE</code> or
<code>NA_INTEGER</code>. This issue only occurs with the clang compiler,
not gcc. When constructing vectors with more than one element this is
not an issue</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="co">// bad</span></span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a>cpp11<span class="op">::</span>writable<span class="op">::</span>logicals<span class="op">({</span>FALSE<span class="op">});</span></span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="co">// good</span></span>
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a>cpp11<span class="op">::</span>writable<span class="op">::</span>logicals<span class="op">({</span>r_bool<span class="op">(</span>FALSE<span class="op">)});</span></span>
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a></span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">// good</span></span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a>cpp11<span class="op">::</span>writable<span class="op">::</span>logicals<span class="op">({</span>FALSE<span class="op">,</span> NA_LOGICAL<span class="op">});</span></span></code></pre></div>
</div>
</div>



<!-- code folding -->


<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>