我想使用的功能模板to_string
轉換一個int
到string
,在C++中是沒有問題的,但如果我在R請勿它,它給了我下面的錯誤:如何在R中通過Rcpp使用C++函數模板?
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
我很困惑,是有替代?
//[[Rcpp::export]]
template <typename T>
std::string to_string(T t)
{
std::ostringstream ss;
ss << t;
return ss.str();
}
感謝信息 – Moritz