[C++-sig] Need help on enum--a concrete example

Min Xu minxu at sci.ccny.cuny.edu
Sat Sep 7 05:06:49 CEST 2002


Hi, 

I understand that the questions about enum had been asked before. I read 
through them to no vail to solve my small problem at hand. From my impression 
of the document implicit.html, I wrote the following code:

#include <boost/python/module_init.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/implicit.hpp>

enum choice {red, blue};

class A
{
public:
	A() : x(0), y(0) {}
	A(double x, choice c) : x(x), y(c) {}
	double get() { return x; }
	double get1(double x) { return x; }
private:
	double x, y;
};


BOOST_PYTHON_MODULE_INIT(AA)
{
 	using namespace boost::python;

	implicitly_convertible<int, enum choice>();

	class_<A>("A")
     	      .def_init(args<double, choice>())
	      .def("get", &A::get)
	      .def("get1", &A::get1);
}


The compilation gave me:
g++ -I/usr/local/include -I/usr/include/python2.2  -c -Wall 
-ftemplate-depth-100  -DBOOST_PYTHON_DYNAMIC_LIB -DBOOST_PYTHON_V2  -fPIC 
AA.C
/usr/local/include/boost/python/converter/implicit.hpp: In function `static 
void boost::python::converter::implicit<int,choice>::construct(PyObject *, 
boost::python::converter::rvalue_from_python_stage1_data *)':
/usr/local/include/boost/python/implicit.hpp:23:   instantiated from 
`boost::python::implicitly_convertible<int, choice>(boost::type<int> *, 
boost::type<choice> *)'
AA.C:24:   instantiated from here
/usr/local/include/boost/python/converter/implicit.hpp:43: conversion from 
`int' to `enum choice'
make: *** [AA.so] Error 1

Thanks!!!


By the way, I have also tried the alternative:
// namespace boost { namespace python {

//     template <class T>
//     struct enum_to_int_converter
//     {
//	static PyObject* convert(T const& x)
//	    {
//		return PyInt_FromLong(x);
//	    }
//     };

//     template <class T>
//     void enum_as_int(T* = 0)
//     {
//	to_python_converter<T, enum_to_int_converter<T> >();
//	implicitly_convertible<int, T>();
//     };

//     // position1:
//     void enum_as_int<choice>();
// }};

It doesnot work either.

Min Xu






More information about the Cplusplus-sig mailing list