[Rcpp-devel] How to handle std::cout/std::cerr in shared libraries

Dirk Eddelbuettel edd at debian.org
Fri Oct 26 19:13:10 CEST 2018


Hi Watal,

Thanks for being patient with me :)

On 27 October 2018 at 01:26, Watal M. Iwasaki wrote:
| Sorry for my poor explanation. I have read
| `inst/include/Rcpp/iostream/Rstreambuf.h` and think I understand the role
| of Rcpp::Rcout, but failed to explain my point. By "users [...] can just
| stick to std::cout", I did not mean allowing users to write to stdout via
| std::cout. It is about changing the destination of std::cout from stdout to
| R's i/o. The following code hopefully explains better:
| 
| ```
| // [[Rcpp::export]]
| void example_function() {
|     // Normal state
|     std::cout   << "to stdout; BAD\n";
|     Rcpp::Rcout << "to R's i/o\n";
| 
|     // Ideal state
|     std::streambuf* stdoutbuf = std::cout.rdbuf(Rcpp::Rcout.rdbuf());
|     std::cout   << "to R's i/o via Rcpp::Rcout.buf; GOOD\n"
|     Rcpp::Rcout << "to R's i/o\n";
| 
|     // Restore original state
|     std::cout.rdbuf(stdoutbuf);
|     std::cout   << "to stdout; BAD\n";
|     Rcpp::Rcout << "to R's i/o\n";
| }
| ```
| 
| In other words, we don't have to replace std::cout with Rcpp::Rcout. We
| only have to change its buffer.

I like it!  I think that may fix it.

It may not fix the detection by R CMD check, but if we can show that we do
the right thing maybe this can be adjusted (to also account for Rcpp and
whatever (hidden) macro we may use to provide this.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list