Boost logo

Boost :

From: Geurt Vos (G.Vos_at_[hidden])
Date: 2001-05-03 10:10:14


>
> On Thursday 03 May 2001 03:26, you wrote:
> > Idea 1:
> > Support for 0 (and only 0) assignment could still be
> > considered, it does however require a change in the
> > interface. Here's the idea: 'function' defines an empty
> > class in its private section, and adds an assignment
> > operator which takes a pointer to this class. Now the
> > user can _only_ assign 0 to it. Here's an example:
> >
> > class function {
> > class assign0 { };
> > public:
> > function& operator=(const assign0 *)
> > {
> > clear();
> > return *this;
> > }
> > };
> >
> > Problems arise when there's an assignment operator
> > which has a parameter that could also be of type
> > 'int', because then 0 assignment will use that
> > operator. In function's case, this means that the
> > assignment operator template has got to go:
> >
> > template<typename Functor>
> > function& operator=([const] Functor [&]f)
>
> I doubt this will work. If I say:
> function<...> f;
> f = 0;
>
> The templated operator= will be invoked because when
> Functor=int, it is a better match (const int& vs. const assign0*).

Exactly, that's why the templated operator= has to be
removed (that was what I was trying to say :).

When there's no op=(const Functor &), assignment to
0 will call op=(const assign0 *). Assignment to a
functor/function will simply call the (non-explicit)
templated constructor.

>
> One additional possibility would be to use the above assign*,
> but declare a
> namespace-level variable "nil" of that type and instead allow
> "f = nil":
>
> namespace {
> const assign0* nil = 0;
> }
>

Yes, but I don't really like this approach, because then
assign0 is too much exposed.

Geurt


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