<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/using-rcpp-with-c11-c14-and-c17/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://gallery.rcpp.org//articles/Rcpp-and-C++11-C++14-C++17/"> Rcpp Gallery</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h3>Background</h3>
<p>When we started the Rcpp Gallery in late 2012, a few of us spent the next four weeks diligently<br>
writing articles ensuring that at least one new article would be posted per day. Two early articles<br>
covered the then-budding support for C++11. Both<br>
<a target="_blank" rel="nofollow" href="http://gallery.rcpp.org/articles/first-steps-with-C++11/">First steps in using C++11 with Rcpp</a> and<br>
<a target="_blank" rel="nofollow" href="http://gallery.rcpp.org/articles/simple-lambda-func-c++11/">A first lambda function with C++11 and Rcpp</a><br>
started by actually showing how to set the compiler directive via the <code>PKG_CXXFLAGS</code> environment<br>
variable.</p>
<p>Both posts were updated a few months later to demonstrate the C++11 plugin that was added in Rcpp<br>
release 0.10.3 in March of 2013, or almost four years ago. Many posts here, on<br>
<a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/tagged/rcpp">StackOverflow</a> and of course on the mailing list<br>
make use of the plugin to tell the compiler to turn on C++11 support. In the early days, this often<br>
meant partial support via (in the case of <code>g++</code>) the <code>-std=c++0x</code> switch. This is still<br>
<a target="_blank" rel="nofollow" href="https://github.com/RcppCore/Rcpp/blob/f99737edbbe699c0128a735518af91a7f1d986da/R/Attributes.R#L464">supported by Rcpp</a><br>
as in particular the Windows side of things was relying on older compilers like g++ version 4.6.*<br>
until more recently (see below).</p>
<p>But some things got a lot better with R 3.1.0. As the<br>
<a target="_blank" rel="nofollow" href="https://stat.ethz.ch/pipermail/r-announce/2014/000572.html">NEWS announcement</a> dryly noted: <em>There<br>
is support for compiling C++11 code in packages on suitable platforms: see ‘Writing R Extensions’.</em><br>
which was coupled with support for selecting the C++11 language standard via either <code>CXX_STD</code> in<br>
<code>src/Makevars</code>, or the <code>SystemRequirements</code> line in <code>DESCRIPTION</code>. In late 2011, Martyn Plummer also<br>
wrote a helpful<br>
<a target="_blank" rel="nofollow" href="https://journal.r-project.org/archive/2011-2/RJournal_2011-2_Plummer.pdf">R Journal article</a> on<br>
best practices for portable C++ packages.</p>
<p>And as we alluded to above, Rcpp has supported C++11 since the dawn of time (because, as we detail<br>
below, it ultimately comes down what your compiler supports, and what R facilitates). Many CRAN<br>
packages have by now taken advantage of this increased support for C++11 in particular. As of<br>
today, we see 88 CRAN packages declaring this via <code>DESCRIPTION</code> and 127 CRAN package via<br>
<code>src/Makevars</code>. And of course, almost all use Rcpp along with C++ to take advantage of the R and<br>
C++ integration it offers.</p>
<h3>Rcpp: Sitting between your Compiler and R</h3>
<p>So what are the defining parameters for support by Rcpp? In essence, Rcpp is guided by just two<br>
(external) factors:</p>
<ul>
<li>
<p>the support in the provided compiler, and</p>
</li>
<li>
<p>the support offered by R for package building,</p>
</li>
</ul>
<p>and both are worth detailing as we do in the next two sections..</p>
<h3>Compiler Support</h3>
<p>First, the choice of compiler matters. Rcpp operates on top of R, and (like any R package) it is<br>
driven entirely by the build instructions from R. It can therefore be dependent on the compiler<br>
choices offered by R. This meant g++ version 4.6.3 for Windows for years. And this got a lot<br>
better when Rtools switched to g++ version 4.9.3 with R 3.3.0 not quite a year ago. It now means<br>
that support for C++11 is almost universal. (Some small things are still missing in <code>g++-4.9.3</code>;<br>
notably complete support for the C++ Standard Library leading us recently to<br>
<a target="_blank" rel="nofollow" href="https://github.com/eddelbuettel/rcppcctz/pull/11">backport <code>get_time</code> from LLVM into RcppCCTZ</a>. Also<br>
note that macOS / OS X has its own dependency chain due to compiler, and release, choices made by<br>
the R package build system for that platform.)</p>
<p>Now, that is just the minimum available compiler for a particular platform, albeit a very important<br>
one as it defines what <em>binary</em> CRAN packages on Windows can support. Other platforms, however,<br>
have a faster release cadence. <code>g++-5</code> and <code>clang++-3.3</code> are now fairly common and have<br>
(near-)complete C++11 support. The most recent Ubuntu release 16.10 even defaults to g++ version<br>
6.2.0, and Debian already has version 6.3.0 (and binaries of version 7.* in its experimental<br>
branch). Similarly, recent version of clang are available both directly in the distributoin and via<br>
nightly builds from dedicated PPAs.</p>
<p>And for these ‘6.*’ version of <code>g++</code>, the default C++ standard is already C++14, the follow-up<br>
standard release to C++11. For example, C++14 extends support for <code>auto</code> to return values so that<br>
we can compile a simple program such as</p>
<figure>
<pre><code data-lang="cpp"><span>#include &lt;iostream>
</span>
<span>auto</span> <span>doubleMe</span><span>(</span><span>const</span> <span>int</span> <span>&amp;</span> <span>x</span><span>)</span> <span>{</span>
    <span>return</span> <span>x</span><span>+</span><span>x</span><span>;</span>
<span>}</span>

<span>int</span> <span>main</span><span>(</span><span>void</span><span>)</span> <span>{</span>
    <span>std</span><span>::</span><span>cout</span> <span>&lt;&lt;</span> <span>&quot;1.0 -> &quot;</span> <span>&lt;&lt;</span> <span>doubleMe</span><span>(</span><span>1.0</span><span>)</span> <span>&lt;&lt;</span> <span>&quot;</span><span>\n</span><span>&quot;</span>
              <span>&lt;&lt;</span> <span>&quot;1 -> &quot;</span> <span>&lt;&lt;</span> <span>doubleMe</span><span>(</span><span>1</span><span>)</span>   <span>&lt;&lt;</span> <span>&quot;</span><span>\n</span><span>&quot;</span>
              <span>&lt;&lt;</span> <span>std</span><span>::</span><span>endl</span><span>;</span>
<span>}</span></code></pre>
</figure>
<p>withour <em>any</em> additional switches or flags if the compiler is as recent as g++ version 6. A plain<br>
<code>g++ -o demoprog demoprog.cpp</code> will do (provided the snippet was saved as file demoprogr.cpp) as<br>
C++14 is the default for this compiler. Otherwise, adding <code>-std=c++14</code> explicitly instruct the<br>
compiler to compile as C++14.</p>
<p>Moreover, it also works for R:</p>
<figure>
<pre><code data-lang="r"><span>Rcpp</span><span>::</span><span>cppFunction</span><span>(</span><span>&quot;auto doubleMe(const int &amp;x) { return x + x; }&quot;</span><span>)</span><span>
</span><span>doubleMe</span><span>(</span><span>1L</span><span>)</span></code></pre>
</figure>
<pre>
[1] 2
</pre>
<figure>
<pre><code data-lang="r"><span>doubleMe</span><span>(</span><span>1</span><span>)</span></code></pre>
</figure>
<pre>
[1] 2
</pre>
<p>(on a machine such as the one used to write this piece) without requiring any additional plugins.<br>
As a reminder, <code>cppFunction()</code> supports these via the <code>plugin=</code> argument. On another machine, we<br>
might use <code>Rcpp::cppFunction(&quot;auto doubleMe(const int &amp;x) { return x + x; }&quot;, plugin=&quot;cpp14&quot;)</code>.<br>
Similarly, in code sourced via <code>sourceCpp()</code> we would use an attribute; see the<br>
<a target="_blank" rel="nofollow" href="https://cloud.r-project.org/package=Rcpp/vignettes/Rcpp-attributes.pdf">Rcpp Attributes</a> vignette<br>
for details.</p>
<h3>Support by R for Packages: C++14 coming soon via R-devel</h3>
<p>As noted above, the R 3.1.0 release started to add support for modern C++ by enabling packages to<br>
select C++11.</p>
<p>The next release will be R 3.4.0. While as of now without an announced release date, it is likely to<br>
be shipped this April. It will bring support for C++14. The current draft of its version of<br>
<em>Writing R Extenions</em> shows that <code>CXX_STD = CXX14</code> can be used to select this language standard in a<br>
package. Several build variables extend support to <code>CXX1Y</code> beyond the existing <code>CXX1X</code>, see the<br>
<em>Writing R Extenions</em> manual for full details.</p>
<p>To illustrate, on a machine with <code>g++</code> version 6.*, nothing has to be turned on for C++14 in what<br>
will be R 3.4.0.</p>
<figure>
<pre><code data-lang="sh"><span>edd@max:~$ </span>grep <span>&quot;^CXX/*STD&quot;</span> /usr/local/lib/R-devel/lib/R/etc/Makeconf 
CXX98STD <span>=</span> -std<span>=</span>gnu++98
CXX1XSTD <span>=</span> -std<span>=</span>gnu++11
CXX1YSTD <span>=</span> 
<span>edd@max:~$ </span></code></pre>
</figure>
<p>On a machine where <code>g++-5</code> is the default, the <code>CXX1XSTD</code> value may be empty as this compiler<br>
defaults to C++11; we would expect <code>CXX1YSTD = -std=c++14</code> there (or the ‘gnu’ variant).</p>
<p>As we noted above, well over one-hundred packages on CRAN already use C++11. We expect to see C++14<br>
being used once R 3.4.0 is released later this spring</p>
<h3>Extensions on the Rcpp side: C++17</h3>
<p>As we wrote in the opening paragraph, a plugin for C++11 has been part of Rcpp for several years.<br>
And a plugin for C++14 was added by Dan in Rcpp 0.12.4 about a year ago.</p>
<p>The current development sources of Rcpp, corresponding to interim GitHub release 0.12.9.3, added a<br>
plugin for C++17 as experimental support exists in g++ and clang++ right now. With that, a suitably<br>
recent compiler, and a version of Rcpp that is at least release 0.12.9.3, the following example<br>
(motivated by this example section of the<br>
<a target="_blank" rel="nofollow" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4480.html#any">C++ Standard post</a>) also<br>
builds:</p>
<figure>
<pre><code data-lang="cpp"><span>#include &lt;experimental/any>
</span>
<span>// [[Rcpp::plugins(cpp17)]] </span>
<span>// [[Rcpp::export]]
</span><span>void</span> <span>useAny</span><span>()</span> <span>{</span>
    <span>std</span><span>::</span><span>experimental</span><span>::</span><span>any</span> <span>x</span><span>(</span><span>5</span><span>);</span>
    <span>// some more here ...
</span>    <span>x</span> <span>=</span> <span>&quot;cat&quot;</span><span>;</span>
<span>}</span></code></pre>
</figure>
<h3>Summary</h3>
<p>This note discussed where Rcpp stands with respect to “modern C++”. As a brief summary:</p>
<ul>
<li>
<p>Rcpp supports any C++ language standard the underlying compiler supports: C++98, C++11, C++14,<br>
C++17;</p>
</li>
<li>
<p>Packages using Rcpp can deploy every language standard suppported by R: currently C++, C++11 and<br>
very soon C++14;</p>
</li>
<li>
<p>Package distribution may need to reflect the build infracture; on Windows this means <code>g++-4.9.3</code><br>
with near-complete C++11 support;</p>
</li>
<li>
<p>Local developement can be more experimental and even C++17 is now supported by Rcpp as well;</p>
</li>
<li>
<p>Portable packages should specify the C++ language standard they expect (unless it is C++98).</p>
</li>
</ul>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/rcpp-gallery/~4/WsrH9Wedc0I"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://gallery.rcpp.org//articles/Rcpp-and-C++11-C++14-C++17/"> Rcpp Gallery</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/--1G_81hJ-g"></a></div>