[C++-sig] problem with constructor/implicitly_convertible

Jonathan Brandmeyer jbrandmeyer at earthlink.net
Fri Feb 4 21:17:50 CET 2005


On Fri, 2005-02-04 at 10:09 -0800, Ralf W. Grosse-Kunstleve wrote:

> > Is there any way to register an implicit conversion from a Python type
> > to a subclass, that will be invoked for functions expecting a base
> > class?  example:
> > 
> > struct Base {};
> > struct Derived : Base {};
> > 
> > void f( const Base& b);
> > 
> > How can I register an implicit converter for Python int -> Derived that
> > will be picked up when f() is called from Python?
> 
> I am not sure I know the answer to this one. I'd probably start experimenting
> by registering custom converters for both int -> Base and int -> Derived.
> 
> Cheers,
>         Ralf

The attached file is the only way that I can think of to register an
int->Base conversion (in this case, I used a double, but that shouldn't
matter since its just for demonstration).

I'm concerned that storage.bytes is only guaranteed to have enough room
for one Base object, and that the placement new operation may be placing
a new object of Derived into an inadequate chunk of storage.  This
example doesn't crash, and Valgrind doesn't report any errors, but I
just don't see how a converter registered for Base can possibly know how
much storage to allocate for Derived.

-Jonathan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rvalue_converters.cpp
Type: text/x-c++src
Size: 1304 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050204/fcceaaf8/attachment.cpp>


More information about the Cplusplus-sig mailing list