[C++-sig] working around the non-const Python C API
David Abrahams
dave at boost-consulting.com
Sat Dec 3 00:35:29 CET 2005
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* ml_name and char* ml_doc
> 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
More information about the Cplusplus-sig
mailing list