Boost logo

Boost Users :

From: qiaozhiqiang_at_[hidden]
Date: 2007-08-10 03:34:42



// Visual V++ 2005(8.0) sp1, boost 1.34.0
// union not is_class.
// because there is BOOST_STATIC_ASSERT(is_class<T>::value) in make_instance_impl
// can not export union. eg, class_<union my_u>
// can not return_internal_reference<> when T not is_class, eg, return char*, char&, union u&
// how to write return_internal_pointer: char* get();
// how to return_value_policy<return_by_value> work: char* get()
// def_readwrite(union): default get() return by value ?
// is_class<T> ? T& : T get() ??

// delete BOOST_STATIC_ASSERT(is_class<T>::value); of make_instance_impl, then compile OK, and work not all OK
boost/python/object/make_instance.hpp
template <class T, class Holder, class Derived>
struct make_instance_impl
{
    typedef objects::instance<Holder> instance_t;
        
    template <class Arg>
    static inline PyObject* execute(Arg& x)
    {
                    // must is_class ?? to BOOST_STATIC_ASSERT(is_class<T>::value || is_union<T>::value)??
        BOOST_STATIC_ASSERT(is_class<T>::value);



///////////////////////////////
#include <boost/python.hpp>
using namespace boost::python;
 
union my_u
{
        int a;
        char b;
        char& get_ref()
        {
                return b;
        }
        char* get_ptr()
        {
                return *b;
        }
}

struct my_s
{
        my_u u;
        my_u& get_ref()
        {
                return u;
        }
        my_u* get_ptr()
        {
                return &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<T>::value) to
    //BOOST_STATIC_ASSERT(is_class<T>::value || is_union<T>::value)
     // and the def_readwrite a, b work OK.
        // and work OK, why not export an union?
        class_<my_u > u_class("my_u", init< >());
        u_class.def_readwrite("a", &my_u::a);
        u_class.def_readwrite("b", &my_u::b);
        
        
        //2. compile ERROR: char not is_class, delete BOOST_STATIC_ASSERT(is_class<T>::value), but run error
        u_class.def("get_ref", &my_u::get_ref, return_internal_reference< >());
        
        //3. compile ERROR: char not is_class, delete BOOST_STATIC_ASSERT(is_class<T>::value), but run error, how to return_internal_reference a char? Use def_readwrite?
        u_class.def("get_ptr", &my_u::get_ptr, return_internal_reference< >());
        
        //4. compile OK, but run ERROR, I need it return a char
        .def("get_value", &my_u::get_ptr,
                 return_value_policy<return_by_value>());
                 
        //5. compile ERROR, char* is not a reference, I need but have no copy_non_const_pointer, how to write copy_non_const_pointer?
        u_class.def("get_copy", &my_u::get_ptr,
                 return_value_policy<copy_non_const_reference>());


        class_<my_s > s_class("my_s", init< >());

        
        //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", &my_s::u);
        
        // 7. compile ERROR: my_u not is_class, is_union, modify BOOST_STATIC_ASSERT(is_class<T>::value) to
        // BOOST_STATIC_ASSERT(is_class<T>::value || is_union<T>::value)
        s_class.def("get_ptr", &my_s::get_ptr, return_internal_reference<>());
        s_class.def("get_ref", &my_s::get_ref, return_value_policy<return_by_value>());//compile ok
}


Best
 Regards !

Qiao Zhi Qiang


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net