[C++-sig] working around the non-const Python C API (original) (raw)
David Abrahams dave at boost-consulting.com
Sat Dec 3 00:35:29 CET 2005
- Previous message: [C++-sig] working around the non-const Python C API
- Next message: [C++-sig] working around the non-const Python C API
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Jeremy Hylton <jeremy at alum.mit.edu> writes:
I was curious if anyone had advice on how to work around the fact that much of the Python C API asks for char* when it really intends to treat it as a const char*. An example of this behavior is the PyMethodDef struct, which has char* mlname and char* mldoc parameters. If you use a string literal, you get a warning about casting a const char* to a char*.
In C++, anyway, these are really supposed to be hard errors. The type of a string literal is char const(&)[N].
I can add a cast around each literal, but that's really ugly.
Is there a good way to avoid the warnings?
Fix Python :)
Secondarily, I know that python-dev is a bit hesitant about const correctness, but we did get some const char* types added with the new bytecode compiler. Perhaps it's worthwhile to try changing a few of the most egregious cases to use const char*.
If you can't do that, the next most good approach is to use const_cast instead of a C-style cast. Just to drive the point home :)
-- Dave Abrahams Boost Consulting www.boost-consulting.com
- Previous message: [C++-sig] working around the non-const Python C API
- Next message: [C++-sig] working around the non-const Python C API
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]