[C++-sig] Re: [boost] Error if member function not called?

David Abrahams dave at boost-consulting.com
Tue Aug 13 20:48:22 CEST 2002


From: "Peter Dimov" <pdimov at mmltd.net>


> From: "David Abrahams" <dave at boost-consulting.com>
> > Hi everybody,
> >
> > I'm looking for a clever trick, if anyone can come up with it...
> > In v2 of the Boost.Python library, when users define a class wrapper,
it
> > looks something like this:
> >
> >     class_<X>("X")
> >         .def_init(args<int, char*>())  // constructor
> >         .def("foo", &X::foo)           // member function
> >         ;
> >
> > Each of class_<>'s member functions returns *this to allow the chaining
> > idiom shown above.
> >
> > What I'd like is to be able to generate a compiler warning or error if
the
> > user fails to call the def_init() member function somewhere in the
chain.
> > Any ideas?
>
> class_<X>("X", args<int, char*>()).def("foo", &X::foo);

I thought of that too, just after I posted. Not a bad idea, since not
passing any args<> can be the same as the default constructor -- that's
actually better than causing an error.

The (minor) problem with this is that it's asymmetric -- the user can add
other constructor overloads, but those will use def_init. Overall, I like
it, though.

This discussion came out of a desire to implement the "Automatic Object
Initialization" feature detailed at http://www.python.org/sigs/c++-sig/.
Opinions from the C++-sig?


-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list