[C++-sig] Re: const char arrays

David Abrahams dave at boost-consulting.com
Tue Aug 5 20:02:57 CEST 2003

"Niall Douglas" <s_sourceforge at nedprod.com> writes:

> Are const char arrays supported by boost.python?

Supported how?

> eg;
>   static const char deleteTypeName[];
> Obviously we'd prefer them to become python strings. 


> BTW MSVC7.1 here 
> stops with "reference to a zero-sized array is illegal". 

That is correct.  The code is ill-formed.  This has nothing whatsoever
to do with Boost.Python.

> BTW a full list of what boost.python doesn't support in the docs
> would be really, really useful - then I can know it's not me
> misconfiguring something.

It would take a long time to write that.  It won't wash your socks or 

> BTW, I've been trying to find docs on how to implement your own 
> custom converters. Basically I'd like to convert a FXString to a 
> python one automatically and back again. I've come up with:
> namespace boost { namespace python {
This macro is not for public consumption; I should probably #undef it
at the end of the header file that defines it.

> PyString_FromStringAndSize(x.text(), x.length()))
> }} // namespace
> For the other direction I tried:
> 	struct FXString_rvalue_from_python
> 	{
> 		static void init()
> 		{
> 			slot_rvalue_from_python<FX::FXString, 
> FXString_rvalue_from_python>();
> 		}
> 		static unaryfunc *get_slot(PyObject *obj)
> 		{
> 			return (PyString_Check(obj)) ? &obj->ob_type->tp_str : 0;
> 		}
> 		static FX::FXString extract(PyObject *intermediate)
> 		{
> 			return FX::FXString(PyString_AsString(intermediate), 
> PyString_Size(intermediate));
> 		}
> 	};
> Problem is that slot_rvalue_from_python<> in builtin_converters.hpp 
> is in an unnamed namespace and so therefore is inaccessible to me :(

I think you should read up on unnamed namespaces again.  Anyway, that
template is in a source file, for good reason.  It's not for public

I suggest you follow the example set in item 2 here:

> Furthermore, string literals in const char * form don't appear to 
> cast to a FXString first as usual. Instead the compiler treats them 
> as a const char *, which AFAICS Boost.python doesn't support (it 
> seems to not to support any pointer to any basic type actually :(
> ). 

It certainly does support char const*, depending on what you mean by
"support".  This function can be wrapped successfully:

      char const* f(char const*);

You can see that it works from libs/python/test/builtin_converters.*

> This is odd because throughout the header files it /looks/ like it 
> support const literals to python strings, but it sure ain't working 
> here (I get a static assertion failure in 
> make_instance_impl::execute() with is_class<T>::value being value).

I assume you mean "being false". Clearly you're using an inappropriate
call policy such as return_internal_reference or

> Any way of working around this? ie; to get const char * literals to 
> become python strings? This library I'm making bindings for works 
> exclusively with C style strings as its lower levels.

There's nothing to work around AFAICT.
Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list