Well, those are good points.  But I just do not know the size of my list a priori, so I have to grow it along the way.  It might not be optimal, but I suppose as you said it is just suboptimal (i.e., not that bad). <div><div>
<br></div><div>Thanks,</div><div>Jiqiang <br><div><br><div class="gmail_quote">On Sun, Dec 9, 2012 at 2:25 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
On 9 December 2012 at 14:07, Jiqiang Guo wrote:<br>
| Note in the above, the printout for the second does not print any attribute as<br>
| for the first function. <br>
<br>
</div>Hm. That it not desirable so maybe we can do better, but you should also note<br>
that you are using the list type in a suboptimal way by growing piece by<br>
piece which leads to __copies__ being made at which point we may cast to<br>
vector and drop the attributes.<br>
<br>
Here is a ever-so-slightlt modified example that works:<br>
<br>
-----------------------------------------------------------------------------<br>
#include <Rcpp.h><br>
<br>
// [[Rcpp::export]]<br>
SEXP crtlist3() {<br>
    Rcpp::List lst(2);   // reserve known size<br>
    lst[0] = 10;<br>
<div class="im">    lst.attr("att1") = 1;<br>
</div>    lst[1] = 20;<br>
    return lst;<br>
}<br>
-----------------------------------------------------------------------------<br>
<br>
R> sourceCpp("/tmp/jiqiang.cpp")<br>
R> crtlist3()<br>
[[1]]<br>
[1] 10<br>
<br>
[[2]]<br>
<div class="im">[1] 20<br>
<br>
attr(,"att1")<br>
[1] 1<br>
</div>R><br>
<br>
<br>
Essentially I do two things differently:  reserve the needed size, and index<br>
by position.  The attr then remains.<br>
<span class="HOEnZb"><font color="#888888"><br>
Dirk<br>
<br>
--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span></blockquote></div><br></div></div></div>