Rcpp 代码在 Mac 上编译但不在 linux 上编译

Rcpp code compiles on Mac but not on linux

我的 C++ 代码是接收一个字符向量并将其转换为 std::set。以下代码在 Mac 上编译但不在 Linux 上编译:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
void test(CharacterVector x) {
  std::set<std::string> vs(x.begin(), x.end());
}

Linux 编译失败:

/usr/include/c++/4.8.2/bits/stl_function.h:481:7: note:   no known conversion for argument 1 from ‘Rcpp::internal::string_proxy<16>’ to ‘const std::basic_string<char>&’

Mac 信息:

R 版本 3.4.1 (2017-06-30) 平台:x86_64-apple-darwin15.6.0(64 位) 运行下:macOS High Sierra 10.13.3 Rcpp_0.12.16

Linux 信息:

R 版本 3.4.2 (2017-09-28) 平台:x86_64-redhat-linux-gnu(64 位) 运行下:CentOSLinux7(核心) Rcpp_0.12.16

作为解决方法,我可以先将字符向量转换为 std::vector,然后将 std::vector 转换为 std::set。但是,这会在我的测试中造成 ~5-10% 的性能损失,我想避免这一点。

这是一个错误吗?如何在 linux 上正确执行此操作?

此变体适用于 Ubuntu 17.10 上的 g++ 7.2:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
void test(std::vector<std::string> x) {
  std::set<std::string> vs(x.begin(), x.end());
}

并运行(但什么都不做)

R> Rcpp::sourceCpp("/tmp/soQ.cpp")
R> test(rep("lalala", 3))
R> 

字符向量很复杂;迭代器可能存在一些不太理想的情况。欢迎细心的PR;与此同时,你有一个简单的选择。