问题
为什么使用std::unordered_map作为函数参数无法编译(以及如何解决它)?
示例
第一个函数将std::unordered_map作为函数参数,但无法编译。
library(Rcpp)
cppFunction(
code = 'void test( std::unordered_map< std::string, std::string > um ) {
}'
, plugins = "cpp11"
)然而,在函数的正文中声明它是很好的
cppFunction(
code = 'void test( ) {
std::unordered_map< std::string, std::string > um;
}'
, plugins = "cpp11"
)额外的
在我的在这里以内联方式库中,我成功地将它用作函数参数spatialwidget函数
理解答案
感谢拉尔夫·斯塔布纳的解释。总之,在使Rcpp函数可由R调用时,必须有对象的等效R表示。
此代码失败,因为R中没有等效的unordered_map。
// [[Rcpp::export]]
void test( std::unordered_map< std::string, std::string > um ) {
}这是因为它没有被调用/导出到R
void test( std::unordered_map< std::string, std::string > um ) {
}发布于 2019-06-05 09:53:40
您可以使用像std::vector和std::list这样的东西作为函数参数,并在可从R调用的函数中返回值,因为Rcpp::as和Rcpp::wrap存在适当的专门化,它们在这些C++数据结构和R所知道的SEXP(两个方向)之间进行转换。现在R没有本地的类似地图的数据类型(尽管我们可以在某种程度上使用命名列表),这可能是Rcpp没有内置的std::unordered_map翻译的原因。对于只能从C++调用的函数没有这样的限制,这就是为什么您的“额外”示例可以工作的原因。
原则上,您可以自己定义这种转换函数,即c.f。http://gallery.rcpp.org/articles/custom-templated-wrap-and-as-for-seamingless-interfaces/和其中的参考资料。但是,您必须首先决定在R端使用哪种类型的数据结构。
https://stackoverflow.com/questions/56456176
复制相似问题