Rcpp CharacterVector:为什么不';t单个元素std::strings?

Rcpp CharacterVector:为什么不';t单个元素std::strings?,string,r,rcpp,String,R,Rcpp,这可能是一个愚蠢的问题,但我认为CharacterVector的每个元素都是std::string。但是,这不起作用: std::string hello(CharacterVector vec) { std::string result = vec[0]; return result; } 错误:候选构造函数不可行:第一个参数没有从“Proxy”(又名“string_Proxy”)到“const_pointer”(又名“const char*”)的已知转换 如果要强制转换为 ST

这可能是一个愚蠢的问题,但我认为
CharacterVector
的每个元素都是
std::string
。但是,这不起作用:

std::string hello(CharacterVector vec) {
   std::string result = vec[0];
   return result;
}
错误:候选构造函数不可行:第一个参数没有从“Proxy”(又名“string_Proxy”)到“const_pointer”(又名“const char*”)的已知转换


如果要强制转换为<代码> STD::String >,在得到C++代码之前,如<代码> EMPTY()/<代码>等。

< P>有时需要帮助编译器。 转换器
Rcpp::as()
从R转换,转换器
Rcpp::wrap()
转换为R


我懂了。转换的缺点是涉及到复制,因此会降低内存效率吗?此外,我在谷歌上搜索了前两页的结果,但没有一个例子直接回答了这个问题。你能给我指出例子的方向吗?我有兴趣了解更多有关模板如何工作的信息。我远不是一个补丁,但我愿意尽我所能提供帮助!:dcopy在内存和执行方面有成本。如果您想为
std::string
编写不同的提取器,您应该在我们的代码和测试中找到示例。FWIW,转换已添加到Rcpp11。关闭窗口。
R> cppFunction('std::string hello(CharacterVector vec) { 
                                return as<std::string>(vec[0]); }')
R> hello(c("The", "quick", "brown", "fox"))
[1] "The"
R> 
R> cppFunction("std::string f(CharacterVector x,int i) {return std::string(x[i]);}")
R> foo(c("The", "quick", "brown", "fox"), 1)
[1] "quick"
R>