【发布时间】:2022-01-06 01:54:18
【问题描述】:
我正在编写一个 R 包 + Rcpp 代码来使用现有的 C++ 库。
在阅读完此处的教程后:https://gallery.rcpp.org/articles/optional-null-function-arguments/,我正在为如何使用 NULL 和字符串而苦苦挣扎。我很困惑,我无法从类型 Rcpp::Nullable<std::string> 转换为 std::string (或等效地从 Rcpp::Nullable<Rcpp::String> 转换为 Rcpp::String
在 C++ 中,我正在检查字符串(在 C++ 中)是否为空。如果字符串为空,我想返回 NULL。如果字符串(在 C++ 中)不为空,我想返回字符串。
我的示例代码如下,为简单起见,修改了Rcpp.package.skeleton()提供的函数rcpp_hello_world()。我的目标是在 R 中返回一个包含字符串或 NULL(如果字符串为空)的列表 (Rcpp::List)。
#include <Rcpp.h>
#include <string>
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::List rcpp_hello_world() {
// After calculations from external C++ library,
// the variable 'mystring' will either empty (i.e. "") or populated (e.g. "helloworld")
std::string mystring = "helloworld"; // string non-empty
Rcpp::Nullable<std::string> result_string = R_NilValue;
if (!mystring.empty()) {
std::string result_string(mystring);
}
Rcpp::List z = List::create(result_string);
return z ;
}
以上示例中的结果变量result_string 应该是NULL 或"mystring"---但是,以上将始终返回NULL,这不是所需的行为。
然后我尝试看看是否可以在Rcpp::Nullable<std::string> 和std::string 之间转换类型:
std::string mystring = "helloworld";
Rcpp::Nullable<std::string> result_string = R_NilValue;
std::string result_string(mystring);
这会导致编译错误:
error: redefinition of 'result_string' with a different type: 'std::string'
(aka 'basic_string<char, char_traits<char>, allocator<char>>') vs 'Rcpp::Nullable<std::string>'
(aka 'Nullable<basic_string<char, char_traits<char>, allocator<char>>>')
我是否为此操作使用了错误的数据结构?或者如果值可以为 NULL,是否有更好的方法来处理字符串?
【问题讨论】:
-
在调用 C++ 代码之前进行所有输入验证,包括检查是否为空
-
@HongOoi 我没有检查 NULL。我正在检查字符串是否为空。如果字符串为空,我想返回 NULL。如果字符串不为空,我想返回字符串。
-
@HongOoi 请让我知道这是否有意义---我无法在 R 中进行此检查;一切都在 C++ 中
-
在调用 C++ 代码之前进行所有输入验证,包括检查空字符串
-
错误信息很清楚。在您的
if语句中,您有一个std::string类型result_string。在 if 语句(范围)之外,std::string对象被破坏,然后您只有Rcpp字符串,因此结果为 NULL。有了这个,我想你现在可以解决原始问题的后半部分了。
标签: c++ r rcpp stdstring r-package