From f5f75c65449ab6956b58626ad13d6530357c2cf9 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 18 Jul 2016 22:47:40 +0100 Subject: [PATCH] Make struct packing in example20 MSVC-compliant --- example/example20.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/example/example20.cpp b/example/example20.cpp index 6557021cf..8b18c05d4 100644 --- a/example/example20.cpp +++ b/example/example20.cpp @@ -13,6 +13,12 @@ #include #include +#ifdef __GNUC__ +#define PYBIND11_PACKED(cls) cls __attribute__((__packed__)) +#else +#define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop)) +#endif + namespace py = pybind11; struct SimpleStruct { @@ -25,20 +31,20 @@ std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) { return os << "s:" << v.x << "," << v.y << "," << v.z; } -struct PackedStruct { +PYBIND11_PACKED(struct PackedStruct { bool x; uint32_t y; float z; -} __attribute__((packed)); +}); std::ostream& operator<<(std::ostream& os, const PackedStruct& v) { return os << "p:" << v.x << "," << v.y << "," << v.z; } -struct NestedStruct { +PYBIND11_PACKED(struct NestedStruct { SimpleStruct a; PackedStruct b; -} __attribute__((packed)); +}); std::ostream& operator<<(std::ostream& os, const NestedStruct& v) { return os << "n:a=" << v.a << ";b=" << v.b; @@ -148,3 +154,5 @@ void init_ex20(py::module &m) { m.def("print_dtypes", &print_dtypes); m.def("get_format_unbound", &get_format_unbound); } + +#undef PYBIND11_PACKED