[Rcpp-devel] returning std::pair using Rcpp

Jelmer Ypma jelmerypma at gmail.com
Mon Oct 10 17:02:39 CEST 2011


Thanks, I just solved my problem by defining a plugin that adds
Rcpp::wrap for std::pair<T1,T2> (see below including some examples). I
couldn't find in which directories/files of the Rcpp package these
wraps are usually defined, otherwise I'd happily supply a patch.
Wrapping a std::pair of std::pair's does not currently work (see
example 3).

Thanks again,
Jelmer

library('inline')
library('Rcpp')

plug <- function(){
	settings <- getPlugin( "Rcpp" )
    settings$includes <- paste(
'
#include <RcppCommon.h>
#include <utility>		// USES pair

// declaring the partial specialization
namespace Rcpp {
	template <typename T1, typename T2> SEXP wrap( const std::pair<T1,T2>& );
}
',
settings$includes,
'
template<typename T1, typename T2> SEXP Rcpp::wrap( const
std::pair<T1,T2>& _v ) {
    return Rcpp::List::create(
        Rcpp::Named("first")  = Rcpp::wrap<T1>( _v.first ),
        Rcpp::Named("second") = Rcpp::wrap<T2>( _v.second )
	);
};
' )
	return( settings )
}
registerPlugin( "RcppWrapPair", plug )


# try to return a std::pair
src_pair1 <- '
std::pair<std::string, int> m( "a" , 2 );
return Rcpp::wrap( m );
'

funx_pair1 <- cxxfunction( signature(), body=src_pair1,
plugin='RcppWrapPair', verbose=TRUE )
funx_pair1()


# try to return a std::pair
src_pair2 <- '
Rcpp::NumericVector v( _v );
std::pair<double, Rcpp::NumericVector> m( 3.141, v );
return Rcpp::wrap( m );
'

funx_pair2 <- cxxfunction( signature( "_v" = "numeric" ),
body=src_pair2, plugin='RcppWrapPair', verbose=TRUE )
funx_pair2( c(2,5,6,2) )



# try to return a pair of pairs (does not work currently)
src_pair3 <- '
std::pair<std::string, int> m1( "a" , 2 );
std::pair<int, double> m2( 3, 2.0 );
std::pair< std::pair<std::string, int>, std::pair<int, double> > m( m1, m2 );
return Rcpp::wrap( m );
'

funx_pair3 <- cxxfunction( signature(), body=src_pair3,
plugin='RcppWrapPair', verbose=TRUE )
funx_pair3()


On Mon, Oct 10, 2011 at 15:45, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 10 October 2011 at 12:26, Jelmer Ypma wrote:
> | Dear list,
> |
> | I tried to return a std::pair using Rcpp and inline (see example
> | below), but didn't succeed. Returning a std::map works fine. Does
> | anyone know whether this does not work because Rcpp::wrap has not been
> | implemented for std::pair or because I'm doing something else wrong?
>
> Quite possibly, yes. Contributions welcome.
>
> Dirk
>
> | The error I get when compiling funx_pair is:
> |
> | In function 'SEXPREC*
> | Rcpp::internal::wrap_dispatch_unknown_iterable(const T&,
> | Rcpp::traits::false_type) [with T = std::pair<std::basic_string<char>,
> | int>, SEXPREC* = SEXPREC*, Rcpp::traits::false_type =
> | Rcpp::traits::integral_constant<bool, false>]':
> |
> | Many thanks in advance,
> | Jelmer
> |
> | library('inline')
> | library('Rcpp')
> |
> | includes <- '
> | #include <utility>            // USES map, pair
> | '
> |
> |
> | # return a std::map (modified from the unit tests)
> | src_map <- '
> | std::map<std::string, int> m;
> | m["a"] = 2;
> | m["b"] = 3;
> | m["c"] = 1;
> | return Rcpp::wrap( m );
> | '
> |
> | funx_map <- cxxfunction( signature(), body=src_map, includes=includes,
> | plugin='Rcpp', verbose=TRUE )
> | funx_map()
> |
> |
> | # try to return a std::pair
> | src_pair <- '
> | std::pair<std::string, int> m( "a" , 2 );
> | return Rcpp::wrap( m );
> | '
> |
> | funx_pair <- cxxfunction( signature(), body=src_pair,
> | includes=includes, plugin='Rcpp', verbose=TRUE )
> | funx_pair()
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
> --
> "Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
> dark to read." -- Groucho Marx
>


More information about the Rcpp-devel mailing list