Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-08-13 13:13:27


----- Original Message -----
From: "David Abrahams" <dave_at_[hidden]>
To: "boost" <boost_at_[hidden]>
Sent: Tuesday, August 13, 2002 2:17 PM
Subject: [boost] Error if member function not called?

> 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?
>
> Thanks,
> Dave
>

Would something like this work for you?

struct A
{
  struct Proxy
  {
    A* obj ;
    Proxy ( A* obj_ ) : obj(obj_) {}

    Proxy& foo() { obj->foo_(); return *this ; }
    Proxy& bar() { obj->bar_(); return *this ; }
  } ;

  Proxy def_init() { return Proxy(this) ; }

  void foo_() {}
  void bar_() {}
} ;

int main()
{
  A().def_init().foo().bar();
  A().foo().bar();

  return 0 ;
}

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk