2 pybind11/complex.h: Complex number support
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
15 /// glibc defines I as a macro which breaks things, e.g., boost template names
20 NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
22 template <typename T> struct format_descriptor<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> {
23 static constexpr const char c = format_descriptor<T>::c;
24 static constexpr const char value[3] = { 'Z', c, '\0' };
25 static std::string format() { return std::string(value); }
28 #ifndef PYBIND11_CPP17
30 template <typename T> constexpr const char format_descriptor<
31 std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>>::value[3];
35 NAMESPACE_BEGIN(detail)
37 template <typename T> struct is_fmt_numeric<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> {
38 static constexpr bool value = true;
39 static constexpr int index = is_fmt_numeric<T>::index + 3;
42 template <typename T> class type_caster<std::complex<T>> {
44 bool load(handle src, bool convert) {
47 if (!convert && !PyComplex_Check(src.ptr()))
49 Py_complex result = PyComplex_AsCComplex(src.ptr());
50 if (result.real == -1.0 && PyErr_Occurred()) {
54 value = std::complex<T>((T) result.real, (T) result.imag);
58 static handle cast(const std::complex<T> &src, return_value_policy /* policy */, handle /* parent */) {
59 return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
62 PYBIND11_TYPE_CASTER(std::complex<T>, _("complex"));
65 NAMESPACE_END(PYBIND11_NAMESPACE)