Skip to content

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

Closed
@bmerry

Description

@bmerry

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions