C++ std::string to Ruby VALUE

C++ std::string to Ruby VALUE

本文关键字:Ruby VALUE to string std C++      更新时间:2023-10-16

如何将C++ std::string对象转换为Ruby VALUE对象?

我尝试了rb_str_new2(c_string),但它不起作用。

我有一个功能

VALUE foo(){return rb_str_new2(c_string);};

这给出了一条错误消息:

cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’

您正在将std::string传递给函数,但它需要以 null 结尾的const char *

std::string::c_str() 成员函数可用于获取一个:

rb_str_new_cstr(string.c_str());