Boost logo

Boost :

From: Doug Gregor (gregod_at_[hidden])
Date: 2001-05-03 09:42:57


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*).
The only way the non-template version will be chosen is if it is an exact
match, i.e., "int" - which allows dumb code like:
f = 10;

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;
}

> Idea 2:
> function is there to take either a function or a
> functor, while bind can be used to turn a member
> function into a functor. I was just wondering,
> wouldn't it be easier (and maybe even more
> consistent) if 'bind' could take either a function
> or a member function, and 'function' only functors?

It would make the coding of boost::function easier, but the resulting code
would be slightly less efficient, since there are no allocations required
when cloning a function pointer.

        Doug


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