Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-11-08 10:06:55


Dan Nuffer <dnuffer_at_[hidden]> writes:

> David Abrahams wrote:
>> Dan Nuffer <dnuffer_at_[hidden]> writes:
>>>10. I added implicitly_convertible<const char*,OW_String>(); It
>>>compiled fine, but it didn't work:
>>> >>> import owclient
>>> >>> s = owclient.OW_String("a")
>>> >>> s.concat(owclient.OW_String("b"))
>>><owclient.OW_String object at 0x817267c>
>>> >>> s.c_str()
>>>'ab'
>>> >>> s.concat("c")
>>>Traceback (most recent call last):
>>> File "<stdin>", line 1, in ?
>>>TypeError: bad argument type for built-in operation
>>>
>>>Any ideas why this didn't work?
>> Not offhand. Can you post a small test case?
>>
>
>
> #include <boost/python.hpp>
>
> using namespace boost::python;
>
> struct test
> {
> test() {}
> test(const char*) {}
> void foo(const test&) {}
> };
>
> BOOST_PYTHON_MODULE(test)
> {
> class_<test>("test")
> .def(init<const char*>())
> .def("foo", &test::foo)
> ;
>
> implicitly_convertible<const char*,test>();
> }
>
>
> >>> import test
> >>> t = test.test("foo")
> >>> t.foo("fooagain")
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: bad argument type for built-in operation

Hmm, this one is subtle. The implicit conversion mechanism is looking
in the converter registry for a ``Python object -> char const*''
converter. But for efficiency in the usual case, that conversion is
handled via specialization, so nothing appears in the converter
registry. I'm not sure what the best solution is; in the long run the
implicit conversion mechanism needs to be reworked anyway. Patches are
always welcome!

In the meantime, there's always the Zen:

   "explicit is better than implicit"

<wink>

-Dave

-- 
                       David Abrahams
   dave_at_[hidden] * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk