如何使用C++;通过Rcpp在R中的函数模板? < >我想使用函数模板 tostring string 将代码>转换为字符串,在C++中没有问题,但是如果我在r中这样做,它会给我以下错误: main.cpp: In function 'std::string to_string(T)': main.cpp:38:11: error: 't' was not declared in this scope ss << t; ^ main.cpp: In function 'SEXPREC* sourceCpp_1_to_string(SEXP)': main.cpp:134:36: error: 'T' was not declared in this scope Rcpp::traits::input_parameter< T >::type tSEXP(tSEXPSEXP); ^ main.cpp:134:38: error: template argument 1 is invalid Rcpp::traits::input_parameter< T >::type tSEXP(tSEXPSEXP); ^ main.cpp:134:46: error: expected initializer before 'tSEXP' Rcpp::traits::input_parameter< T >::type tSEXP(tSEXPSEXP); ^ main.cpp:135:44: error: 'tSEXP' was not declared in this scope rcpp_result_gen = Rcpp::wrap(to_string(tSEXP)); ^ make: *** [main.o] Error 1 main.cpp:std::string to_string(T)函数中: main.cpp:38:11:错误:未在此作用域中声明“t” ss::类型tSEXP(tSEXPSEXP); ^ main.cpp:134:38:错误:模板参数1无效 Rcpp::traits::input_参数::类型tSEXP(tSEXPSEXP); ^ main.cpp:134:46:错误:“tSEXP”之前应为初始值设定项 Rcpp::traits::input_参数::类型tSEXP(tSEXPSEXP); ^ main.cpp:135:44:错误:“tSEXP”未在此范围内声明 rcpp_result_gen=rcpp::wrap(到字符串(tSEXP)); ^ make:**[main.o]错误1

如何使用C++;通过Rcpp在R中的函数模板? < >我想使用函数模板 tostring string 将代码>转换为字符串,在C++中没有问题,但是如果我在r中这样做,它会给我以下错误: main.cpp: In function 'std::string to_string(T)': main.cpp:38:11: error: 't' was not declared in this scope ss << t; ^ main.cpp: In function 'SEXPREC* sourceCpp_1_to_string(SEXP)': main.cpp:134:36: error: 'T' was not declared in this scope Rcpp::traits::input_parameter< T >::type tSEXP(tSEXPSEXP); ^ main.cpp:134:38: error: template argument 1 is invalid Rcpp::traits::input_parameter< T >::type tSEXP(tSEXPSEXP); ^ main.cpp:134:46: error: expected initializer before 'tSEXP' Rcpp::traits::input_parameter< T >::type tSEXP(tSEXPSEXP); ^ main.cpp:135:44: error: 'tSEXP' was not declared in this scope rcpp_result_gen = Rcpp::wrap(to_string(tSEXP)); ^ make: *** [main.o] Error 1 main.cpp:std::string to_string(T)函数中: main.cpp:38:11:错误:未在此作用域中声明“t” ss::类型tSEXP(tSEXPSEXP); ^ main.cpp:134:38:错误:模板参数1无效 Rcpp::traits::input_参数::类型tSEXP(tSEXPSEXP); ^ main.cpp:134:46:错误:“tSEXP”之前应为初始值设定项 Rcpp::traits::input_参数::类型tSEXP(tSEXPSEXP); ^ main.cpp:135:44:错误:“tSEXP”未在此范围内声明 rcpp_result_gen=rcpp::wrap(到字符串(tSEXP)); ^ make:**[main.o]错误1,c++,r,rcpp,C++,R,Rcpp,我很困惑,还有其他选择吗 //[[Rcpp::export]] template <typename T> std::string to_string(T t) { std::ostringstream ss; ss << t; return ss.str(); } /[[Rcpp::export]] 模板 标准::字符串到_字符串(T) { std::ostringstream ss; SSR基本上不知道模板,实际上,R和C++之间的接口是C,

我很困惑,还有其他选择吗

//[[Rcpp::export]]
template <typename T>
std::string to_string(T t)
{
    std::ostringstream ss;
    ss << t;
    return ss.str();
}
/[[Rcpp::export]]
模板
标准::字符串到_字符串(T)
{
std::ostringstream ss;

SS

R基本上不知道模板,实际上,R和C++之间的接口是C,所以所有C限制都适用。


因此,您不仅不能导出函数模板,也不能有意义地调用未导出的模板,因为您从R获得的类型是动态运行时类型,而不是静态类型。您需要执行运行时类型分派。

关于您尝试失败的原因,我没有什么要补充Konrad的答案;但是从过去的ex我建议(编辑:见下面的更新)对于转换遵循
Rcpp::as
,而不是
std::ostringstream
,因为前者依赖于C级
Rf\u强制执行器(IIRC),因此应产生与R的
as.character
(例如,浮点、
Date
POSIXt
值)。在任何一种情况下,都是您的朋友,消除了一堆样板文件:

#include <Rcpp.h>
using namespace Rcpp;

template <int RTYPE>
CharacterVector as_character(const Vector<RTYPE>& x) {
    return as<CharacterVector>(x);
}

// [[Rcpp::export]]
SEXP to_string(SEXP t)
{
    RCPP_RETURN_VECTOR(as_character, t);
}

/*** R

to_string(1)
# [1] "1"

to_string(1.5)
# [1] "1.5"

to_string(1.5 + 2i)
# [1] "1.5+2i"

to_string(TRUE)
# [1] "TRUE"

to_string("abc")
# [1] "abc"

to_string(Sys.Date())
# [1] "2017-07-18"

to_string(Sys.time())
# [1] "2017-07-18 06:48:58"

*/
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
CharacterVector to_string(SEXP t) {
    return CharacterVector(t);
}