<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 2/26/2015 16:37, Pierre GLOAGUEN
      wrote:<br>
    </div>
    <blockquote cite="mid:54EF3DB4.7000706@ifremer.fr" type="cite">
      <meta content="text/html; charset=windows-1252"
        http-equiv="Content-Type">
      Thanks, it works!<br>
      I'm not familiar with C++, is it necessary to always use such a
      function in C++ or is it because of R/C++ interface?<br>
    </blockquote>
    Hi Pierre!<br>
    <br>
    You're witnessing yet another fascinating difference between (usual
    in the world of C++) value semantics and (not that usual in the
    world of C++) reference semantics:<br>
    Compare the difference in behavior of `vector_reference` (using
    `Rcpp::NumericVector`) and `vector_value` (using
    `std::vector<double>`) in the code attached below.<br>
    <br>
    To make things clear: no, in C++ you're not (not by default and not
    in the standard world) supposed to care about (nor use) a function
    such as `clone` -- this is idiomatic in programming languages with
    reference semantics, like Java:
    <a class="moz-txt-link-freetext" href="https://en.wikipedia.org/wiki/Clone_%28Java_method%29">https://en.wikipedia.org/wiki/Clone_%28Java_method%29</a><br>
    <br>
    If this makes you feel better: If you're coming from C++ your
    expectations are perfectly reasonable and this is how any
    well-designed (as in: not violating the POLS) C++ library (including
    the C++ standard library) will behave by default.<br>
    Which incidentally brings me to the advice I usually give in these
    situations: unless you're absolutely dependent on the "features" of
    `Rcpp::NumericVector` just forget about it and replace all uses with
    the standard container `std::vector<double>`.<br>
    // Arguably, NumericVector's POLS violation -- as illustrated by the
    numerous posts raising similar questions -- perhaps isn't
    necessarily the optimal design choice?<br>
    <br>
    Everything will behave just as you expect and as well-documented in
    the usual places -- e.g.,
    <a class="moz-txt-link-freetext" href="http://en.cppreference.com/w/cpp/container/vector">http://en.cppreference.com/w/cpp/container/vector</a><br>
    In contrast, `help.search("Rcpp::NumericVector")` happily announces
    "No results found" at the moment -- and, as mentioned in another
    reply, you're apparently expected to Google around to find methods
    for solving problems you wouldn't even encounter otherwise.<br>
    As fun as it can be to learn-via-a-search-engine, if you don't find
    the curiosities like the aforementioned reference-vs-value-semantics
    differences _all_ _that_ fascinating, I think you may find yourself
    more productive and better served by sticking to the standard
    containers :-)<br>
    <br>
    HTH!<br>
    <br>
    #include <Rcpp.h><br>
    <br>
    // [[Rcpp::export]]<br>
    Rcpp::NumericVector vector_reference(Rcpp::NumericVector input)<br>
    {<br>
        Rcpp::NumericVector output(input);<br>
        if (output.size() >= 1) output[0] = 123;<br>
        return output;<br>
    }<br>
    <br>
    // [[Rcpp::export]]<br>
    std::vector<double> vector_value(const
    std::vector<double> & input)<br>
    {<br>
        std::vector<double> output(input);<br>
        if (output.size() >= 1) output[0] = 123;<br>
        return output;<br>
    }<br>
    <br>
    /*** R<br>
    v = rep(1, 6)<br>
    vector_reference(v)<br>
    show(v)<br>
    <br>
    v = rep(1, 6)<br>
    vector_value(v)<br>
    show(v)<br>
    <br>
    #Output:<br>
    <br>
    #> v = rep(1, 6)<br>
    #> vector_reference(v)<br>
    #[1] 123   1   1   1   1   1<br>
    #> show(v)<br>
    #[1] 123   1   1   1   1   1<br>
    <br>
    #> v = rep(1, 6)<br>
    #> vector_value(v)<br>
    #[1] 123   1   1   1   1   1<br>
    #> show(v)<br>
    #[1] 1 1 1 1 1 1<br>
    */<br>
    <br>
    Best,<br>
    <br>
    Matt<br>
    <blockquote cite="mid:54EF38E1.1060305@ifremer.fr" type="cite"> </blockquote>
    <blockquote cite="mid:54EF3DB4.7000706@ifremer.fr" type="cite">
      Thanks again for your help,<br>
      <br>
      Pierre<br>
      <br>
      Le 26/02/2015 16:30, Jeffrey Pollock a écrit :
      <blockquote
cite="mid:CAG992jJy2yRVAXa-USvjFqV4HeDMghgvMoPpQ_qPSeoLK6VD5g@mail.gmail.com"
        type="cite">
        <div dir="ltr">Perhaps use the clone() function?<br>
          <br>
          library(Rcpp)<br>
          <br>
          cppFunction("<br>
          NumericVector par_CMAtR(NumericVector vec_CMA) {<br>
              NumericVector out = clone(vec_CMA);<br>
              out[5] = exp(out[5]);<br>
              return out;<br>
          }<br>
          ")<br>
          <br>
          vec_C <- rep(1, 6)<br>
          par_CMAtR(vec_C)<br>
          print(vec_C)<br>
        </div>
        <div class="gmail_extra"><br>
          <div class="gmail_quote">On Thu, Feb 26, 2015 at 3:16 PM,
            Pierre GLOAGUEN <span dir="ltr"><<a
                moz-do-not-send="true"
                href="mailto:Pierre.Gloaguen@ifremer.fr" target="_blank">Pierre.Gloaguen@ifremer.fr</a>></span>
            wrote:<br>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              <div bgcolor="#FFFFFF" text="#000000"> <br>
                Hello everybody,<br>
                <br>
                I have a very simple example<br>
                I have a vector vec_CMA which length is a multiple of 6.
                <br>
                I want to get the exact same vector, except the last
                element which is the exponential of the last element of
                vec_CMA<br>
                The code is the following<br>
                <br>
                //myfun.cpp<br>
                #include <Rcpp.h><br>
                using namespace Rcpp;<br>
                <br>
                // [[Rcpp::export]]<br>
                NumericVector par_CMAtR(NumericVector vec_CMA){<br>
                  int K = (vec_CMA.size())/6;<br>
                  NumericVector out(6*K);<br>
                  out = vec_CMA;<br>
                  out[6*K-1] = exp(vec_CMA[6*K-1]);<br>
                  return out;<br>
                }<br>
                <br>
                I apply the function with the R code<br>
                sourceCpp("myfun.cpp")<br>
                vec_C <- rep(1,6)<br>
                par_CMAtR(vec_C)<br>
                [1] 1 1 1 1 1 2.718282<span
                  style="border-collapse:separate;color:rgb(248,248,248);font-family:'DejaVu

                  Sans
Mono';font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:15px;text-align:-webkit-left;text-indent:0px;text-transform:none;white-space:pre-wrap;word-spacing:0px;background-color:rgb(20,20,20)">
                  <pre style="font-family:'DejaVu Sans Mono';font-size:10pt!important;outline-style:none;outline-width:initial;outline-color:initial;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;white-space:pre-wrap!important;word-break:break-all;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:1.2">8</pre>
                </span>works fine. Except the vec_C is modified too!<br>
                vec_C<br>
                [1] 1 1 1 1 1 2.718282<br>
                <br>
                It's the first time I have this kind of problem. What is
                wrong in my code?<br>
                Thanks for your help,<br>
                <br>
                Pierre Gloaguen<br>
                <br>
              </div>
              <br>
              _______________________________________________<br>
              Rcpp-devel mailing list<br>
              <a moz-do-not-send="true"
                href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
              <a moz-do-not-send="true"
href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel"
                target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
            </blockquote>
          </div>
          <br>
        </div>
      </blockquote>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Rcpp-devel mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></pre>
    </blockquote>
    <br>
  </body>
</html>