Skip to content

PYBIND11_NUMPY_DTYPE doesn't work on structures containing std::complex #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bmerry opened this issue Apr 16, 2017 · 2 comments
Closed

Comments

@bmerry
Copy link
Contributor

bmerry commented Apr 16, 2017

Issue description

I'd like to use PYBIND11_NUMPY_DTYPE to wrap a structure which has a std::complex<float> member. I get the error

.../env/include/site/python3.5/pybind11/numpy.h: In instantiation of ‘struct pybind11::detail::npy_format_descriptor<foo>’:
pybind11-array.cpp:19:5:   required from here
.../env/include/site/python3.5/pybind11/numpy.h:985:5: error: static assertion failed: Attempt to use a non-POD or unimplemented POD type as a numpy dtype
     static_assert(is_pod_struct<T>::value, "Attempt to use a non-POD or unimplemented POD type as a numpy dtype");

I'm not sure if this is completely fixable, because std::complex has a non-trivial default constructor (initialising to 0+0i), meaning that it can never be trivial and hence not POD.

Possibly it can be addressed by weakening the static_assert so that it allows through cases like this, and trusting the user not to do something silly. For example, it might be weakened to only require that the type is standard-layout and trivially copyable rather than POD (I couldn't find a guarantee in N4296 that std::complex is standard-layout, only that it is literal, but I can't imagine an implementation that isn't standard-layout yet adheres the layout compatibility with T[2]).

Reproducible example code

#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <pybind11/numpy.h>
#include <array>
#include <cstdint>
#include <complex>
#include <type_traits>

namespace py = pybind11;

struct foo
{
    std::int32_t integral;
    std::complex<float> cplex;
};

PYBIND11_PLUGIN(arraytest)
{
    PYBIND11_NUMPY_DTYPE(foo, integral, cplex);

    py::module m("arraytest", "doc");
    return m.ptr();
}

Compiled with g++ -std=c++14 -c pybind11-array.cpp plus -I options for my installation of Python and pybind11.

@jagerman
Copy link
Member

Any such implementation would break anyway: we explicitly allow single std::complex<T> values (for T in double, float, long double) which numpy is going to read/write to directly as a T[2]. The issue is mainly how to allow them when they are in a struct.

@dean0x7d
Copy link
Member

Resolved via #831.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants