[C++-sig] IndexError when returning pointer

Dave Abrahams dave at boost-consulting.com
Tue Apr 15 16:22:56 CEST 2003


On Friday, April 11, 2003 3:25 PM [GMT+1=CET],
Marcelo A. Camelo <camelo at esss.com.br> wrote:

> Oops,
>
> I've made a terrible mess on the sample code
> I've sent on my last message. Sorry!!!
>
> Here is the correct version:
>
> struct foo
> {
> static foo* get_singleton() { return singleton; }
> static foo* singleton;
> };
>
> foo* foo::singleton = NULL;
>
> BOOST_PYTHON_MODULE( frog )
> {
> using namespace boost::python;
>
> class_< foo, boost::noncopyable >( "foo", no_init )
> .def( "get_singleton", (foo*(*)() )
                         ^^^^^^^^^^^^
> &foo::get_singleton, return_internal_reference<>() )
> .staticmethod( "get_singleton" )
> ;
> }
>
>
> > > > import frog
> > > > a = frog.foo.get_singleton()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> IndexError: tuple index out of range

It's got nothing to do with your problem, but it's usually bad form to use a
cast (especially a C-style cast) when it's not needed.


-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list