[Rcpp-devel] Typecasting bug in Rcpp::List (generic_name_proxy) when using clang
Christian Authmann
authmanc at Mathematik.Uni-Marburg.de
Tue Jul 29 10:36:15 CEST 2014
Hi,
the following testcase exposes a difference in gcc and clang
-------------------- snip --------------------
#include <Rcpp.h>
#include <RInside.h>
int main(int argc, char *argv[]) {
// We need some kind of R environment to create a List.
RInside R;
Rcpp::List list;
list["a"] = 42;
int a_auto = list["a"];
int a_cast = Rcpp::as<int>(list["a"]);
std::cout << "list[\"a\"] = " << a_auto << std::endl;
std::cout << "Rcpp::as<int>(list[\"a\"]) = " << a_cast << std::endl;
}
-------------------- snip --------------------
Results:
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
list["a"] = 42
Rcpp::as<int>(list["a"]) = 42
Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
list["a"] = 1
Rcpp::as<int>(list["a"]) = 42
list["a"] returns a Rcpp::internal::generic_name_proxy<19>, which has
the following typecasting operators: (rcpp/vector/proxy.h)
-------------------- snip --------------------
template <typename T>
operator T() const {
#if RCPP_DEBUG_LEVEL > 0
SEXP res = get() ;
RCPP_DEBUG_1( "generic_name_proxy::get() = <%p> ", res ) ;
return ::Rcpp::as<T>( res ) ;
#else
return ::Rcpp::as<T>( get() ) ;
#endif
}
operator bool() const{
return ::Rcpp::as<bool>(get());
}
-------------------- snip --------------------
A few sprinkled printf's reveal that clang will call the bool typecast,
while gcc calls the template as intended.
Now that I know, I can easily work around it by using Rcpp::as where
needed (and I'm posting here so maybe others can find the information),
but maybe it should be fixed. ;)
I don't know all the intricacies of the C++ standard. Is this a bug in
clang? Or is clang free to use any conversion operator it likes, and
Rcpp is at fault for relying on implementation details of gcc?
If the former, I'll create a reduced testcase and file a bug with clang.
If the latter, I noticed that removing the bool typecast altogether will
cause clang to correctly return 42. Unless there is a good reason for
its existence (say, to suppress logging for bool casts), we could just
remove it.
Grep'ping all the includes suggest that this is the only class with
redundant typecast operators.
Ciao,
Christian
--
Christian Authmann
Philipps-Universität Marburg
Fachbereich Mathematik und Informatik
AG Datenbanksysteme
Hans-Meerwein-Straße
D-35032 Marburg
More information about the Rcpp-devel
mailing list