[Rcpp-devel] Using C++0x in g++ changes ifelse behaviour

Darren Cook darren at dcook.org
Tue Aug 16 04:10:07 CEST 2011


I posted this on StackOverflow yesterday, but maybe of more interest to
people here:
  How to set g++ compiler flags using Rcpp and inline?
  http://stackoverflow.com/q/7063265/841830

In lieu of an answer I created ~/.R/Makevars with this single line:
  CXXFLAGS = -std=c++0x

That works, but then I realized that ifelse is behaving differently today!

This simple example I posted yesterday:
  using namespace Rcpp;
  NumericMatrix x(x_);
  return wrap(ifelse(x<5,x*x,x));

returned this before:
  1  4  9 16  5  6  7  8  9 10 11 12

but in c++0x mode (g++ 3.4) it returns:
  0  0  0  0  5  6  7  8  9 10 11 12

Then, Christopher's reply [1] works with normal g++, but crashes with
c++0x mode (see [2] for the error). As I played around with the code I
sometimes got garbage values in the returned matrix instead of a crash.
(-std=gnu++0x behaves the same.)

I took a look at the Rcpp source and the only thing that came to mind is
that exception specifications have been deprecated but the Rcpp code is
using them everywhere. I don't see how that could explain the observed
behaviour though, so I think it must be something more obscure?

Does everyone see the problem with different C++0x compilers, and/or
later versions of g++?

Darren


[1]:
using namespace Rcpp;
NumericMatrix x(x_);
NumericVector y=ifelse(x<5,x*x,x);
y.attr("dim")=Dimension(x.nrow(),x.ncol());
return y;

[2]:
 *** caught segfault ***
address 0x300000000, cause 'memory not mapped'

Traceback:
 1: .Primitive(".Call")(<pointer: 0x7f36acb327e6>, x_)
 2: fun(x)
 3: print(fun(x))
 4: eval.with.vis(expr, envir, enclos)
 5: eval.with.vis(ei, envir)
 6: source("test4.R")

-- 
Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)


More information about the Rcpp-devel mailing list