Python 到 C/C++常量字符问题

发布于 2024-08-28 03:26:24 字数 915 浏览 8 评论 0原文

我正在用一些 C++ 代码扩展 Python。

我正在使用的函数之一具有以下签名:(

int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
                                char *format, char **kwlist, ...);

链接:http ://docs.python.org/release/1.5.2p2/ext/parseTupleAndKeywords.html

感兴趣的参数是kwlist。在上面的链接中,给出了如何使用此功能的示例。在示例中,kwlist 如下所示:

  static char *kwlist[] = {"voltage", "state", "action", "type", NULL};

当我使用 g++ 编译它时,我收到警告:

warning: deprecated conversion from string constant to ‘char*’

因此,我可以将 static char* 更改为 static const char*。不幸的是,我无法更改Python 代码。因此,通过此更改,我得到了不同的编译错误(无法将 char** 转换为 const char**)。根据我在这里读到的内容,我可以打开编译器标志来忽略警告,或者可以将 kwlist 定义中的每个常量字符串转换为 char *。目前,我正在做后者。还有哪些其他解决方案?

抱歉,如果这个问题之前已被问过。我是新来的。

I am extending Python with some C++ code.

One of the functions I'm using has the following signature:

int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
                                char *format, char **kwlist, ...);

(link: http://docs.python.org/release/1.5.2p2/ext/parseTupleAndKeywords.html)

The parameter of interest is kwlist. In the link above, examples on how to use this function are given. In the examples, kwlist looks like:

  static char *kwlist[] = {"voltage", "state", "action", "type", NULL};

When I compile this using g++, I get the warning:

warning: deprecated conversion from string constant to ‘char*’

So, I can change the static char* to a static const char*. Unfortunately, I can't change the Python code. So with this change, I get a different compilation error (can't convert char** to const char**). Based on what I've read here, I can turn on compiler flags to ignore the warning or I can cast each of the constant strings in the definition of kwlist to char *. Currently, I'm doing the latter. What are other solutions?

Sorry if this question has been asked before. I'm new.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰認得朕 2024-09-04 03:26:24

PyArg_ParseTupleAndKeywords() 是否期望修改您传入的数据?通常,在惯用的 C++ 中,const表示* 指向被调用者只能读取的对象,而 则指向被调用者只能读取的对象。 * 指向被调用者可以写入的对象。

如果 PyArg_ParseTupleAndKeywords() 期望能够写入您传入的 char * ,那么除了您在问题中提到的问题之外,您还会遇到一个完全不同的问题。

假设 PyArg_ParseTupleAndKeywords 不想修改其参数,处理此问题的惯用正确方法是将 kwlist 声明为 const char *kwlist[] 并使用>const_cast 在调用 PyArg_ParseTupleAndKeywords() 时删除其常量性,这将使其看起来像这样:

PyArg_ParseTupleAndKeywords(..., ..., ..., const_cast<char **>(kwlist), ...);

Does PyArg_ParseTupleAndKeywords() expect to modify the data you are passing in? Normally, in idiomatic C++, a const <something> * points to an object that the callee will only read from, whereas <something> * points to an object that the callee can write to.

If PyArg_ParseTupleAndKeywords() expects to be able to write to the char * you are passing in, you've got an entirely different problem over and above what you mention in your question.

Assuming that PyArg_ParseTupleAndKeywords does not want to modify its parameters, the idiomatically correct way of dealing with this problem would be to declare kwlist as const char *kwlist[] and use const_cast to remove its const-ness when calling PyArg_ParseTupleAndKeywords() which would make it look like this:

PyArg_ParseTupleAndKeywords(..., ..., ..., const_cast<char **>(kwlist), ...);
划一舟意中人 2024-09-04 03:26:24

七年前有一个被接受的答案,但我想添加一个替代解决方案,因为这个主题似乎仍然相关。


如果您不喜欢 const_cast 解决方案,您还可以创建字符串数组的可写版本。

char s_voltage[] = "voltage";
char s_state[] = "state";
char s_action[] = "action";
char s_type[] = "type";
char *kwlist[] = {s_voltage, s_state, s_action, s_type, NULL};

char name[] = ".." 将字符串复制到可写位置。

There is an accepted answer from seven years ago, but I'd like to add an alternative solution, since this topic seems to be still relevant.


If you don't like the const_cast solution, you can also create a write-able version of the string array.

char s_voltage[] = "voltage";
char s_state[] = "state";
char s_action[] = "action";
char s_type[] = "type";
char *kwlist[] = {s_voltage, s_state, s_action, s_type, NULL};

The char name[] = ".." copies the your string to a writable location.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文