[C++-sig] Re: std::complex<double>?

Neal D. Becker nbecker at hns.com
Fri Jan 9 16:36:07 CET 2004


Raoul Gough wrote:

> David Abrahams <dave at boost-consulting.com> writes:
> 
>> "Neal D. Becker" <nbecker at hns.com> writes:
> [snip]
>>> z=CDoubleVec(2)
>>>>>> z
>>> <hello.CDoubleVec object at 0x8446a54>
>>>>>> z[0]
>>> Traceback (most recent call last):
>>>   File "<stdin>", line 1, in ?
>>> TypeError: No Python class registered for C++ class std::complex<double>
>>
>> That's because the vector_indexing_suite is trying to treat
>> std::complex like any other class, instead of like an int (i.e. a
>> type with an immutable Python counterpart).  I don't know what the
>> answer to that is, though.  Joel/Raoul?
>>
>>> I also will need std::complex<int>.  What would I need for this?
>>
>> complex<int> isn't even guaranteed to work by the C++ standard.  You'd
>> need to explicitly register to/from-python converters for it.
> 
> I think what Jonathan Brandmeyer was suggesting would do the
> trick. The indexing suite(s) just assume that the contained type is
> already known to Python, and the easiest way to do that is to create a
> boost::python::class_ instance for it.
> 

I noticed in boost.cvs/boost/python/converter/builtin_converters.hpp:
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<float>, ::PyComplex_FromDoubles(x.real(),
x.imag()))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<double>, ::PyComplex_FromDoubles(x.real(),
x.imag()))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<long
double>, ::PyComplex_FromDoubles(x.real(), x.imag()))

Does this suggest that some of the machinery is already there?





More information about the Cplusplus-sig mailing list