Boost C++ Libraries: Ticket #1179: [boost.python] can not export a union in VC2005sp1 https://svn.boost.org/trac10/ticket/1179 <pre class="wiki">// Visual V++ 2005(8.0) sp1, union not is_class. // because there is BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value) in make_instance_impl // can not export union. eg, class_&lt;union my_u&gt; // can not return_internal_reference&lt;&gt; when not is_class, eg, return char*, char&amp;, union u&amp; // delete BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value); of make_instance_impl, then compile OK, and work not all OK boost/python/object/make_instance.hpp template &lt;class T, class Holder, class Derived&gt; struct make_instance_impl { typedef objects::instance&lt;Holder&gt; instance_t; template &lt;class Arg&gt; static inline PyObject* execute(Arg&amp; x) { // must is_class ?? BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value || is_union&lt;T&gt;::value); BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value); /////////////////////////////// #include &lt;boost/python.hpp&gt; using namespace boost::python; union my_u { int a; char b; char&amp; get_ref() { return b; } char* get_ptr() { return *b; } } struct my_s { my_u u; my_u&amp; get_ref() { return u; } my_u* get_ptr() { return &amp;u; } }; void my_module() { //1. compile ERROR: my_u not is_class, my_u is_union, I modify one line in make_instance.hpp // BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value) to BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value || is_union&lt;T&gt;::value) // and work OK, why not export an union? class_&lt;my_u &gt; u_class("my_u", init&lt; &gt;()); u_class.def_readwrite("a", &amp;my_u::a); u_class.def_readwrite("b", &amp;my_u::b); //2. compile ERROR: char not is_class, delete BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value), but run error u_class.def("get_ref", &amp;my_u::get_ref, return_internal_reference&lt; &gt;()); //3. compile ERROR: char not is_class, delete BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value), but run error u_class.def("get_ptr", &amp;my_u::get_ptr, return_internal_reference&lt; &gt;()); //4. compile OK, but run ERROR, I need it return a char .def("get_value", &amp;my_u::get_ptr, return_value_policy&lt;return_by_value&gt;()); //5. compile ERROR, char* is not a reference, I need but have no copy_non_const_pointer u_class.def("get_copy", &amp;my_u::get_ptr, return_value_policy&lt;copy_non_const_reference&gt;()); class_&lt;my_s &gt; s_class("my_s", init&lt; &gt;()); //6. compile OK, buy run ERROR: // s = my_s() // s.u.a = 100 // print s.u.a // but output s.u.a != 100 !!! // s.u.a = 100 is s.get_u().set_a(100) and get_u() not return u(is_union) by ref ? s_class.def_readwrite("u", &amp;my_s::u); // 7. compile ERROR: my_u not is_class, is_union, modify BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value) to // BOOST_STATIC_ASSERT(is_class&lt;T&gt;::value || is_union&lt;T&gt;::value) s_class.def("get_ptr", &amp;my_s::get_ptr, return_internal_reference&lt;&gt;()); s_class.def("get_ref", &amp;my_s::get_ref, return_value_policy&lt;return_by_value&gt;());//compile ok } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1179 Trac 1.4.3 Dave Abrahams Thu, 26 Jun 2008 20:55:11 GMT description changed https://svn.boost.org/trac10/ticket/1179#comment:1 https://svn.boost.org/trac10/ticket/1179#comment:1 <ul> <li><strong>description</strong> modified (<a href="/trac10/ticket/1179?action=diff&amp;version=1">diff</a>) </li> </ul> <p> Fix formatting mess </p> Ticket Dave Abrahams Thu, 26 Jun 2008 20:56:40 GMT status, milestone changed https://svn.boost.org/trac10/ticket/1179#comment:2 https://svn.boost.org/trac10/ticket/1179#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.36.0</span> </li> </ul> <p> OK, looks like there's an easy and reasonable low-risk fix. </p> Ticket Dave Abrahams Sat, 28 Nov 2009 16:37:41 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/1179#comment:3 https://svn.boost.org/trac10/ticket/1179#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> I'm sorry; I don't know what sort of fix I had in mind, but this code <em>couldn't</em> compile as given, even if Boost.Python were changed as requested. </p> <ol><li> There are several syntax errors. </li></ol><ol start="2"><li>You can only <code>return_internal_reference&lt;&gt;</code> to a wrapped class type, not a char; don't forget, char gets translated into a Python string and Python strings are immutable. </li></ol><ol start="3"><li>You're using <code>copy_non_const_reference</code> on a pointer to char, but it intentionally only works on a pointer to reference. </li></ol><p> If you can give me an example that doesn't try to do things that are intentionally illegal (other than wrapping a union for Python) then I might be able to try to make this work. Otherwise, closing as invalid. </p> Ticket