From: Doug Gregor (gregod_at_[hidden])
Date: 2001-04-30 23:02:08


On Monday 30 April 2001 05:34, you wrote:
> i have a question about the just uploaded function
> Callback/function-04-29.zip by Doug Gregor (great stuff btw):
> is it possible to make the operator () methods const? or is/was there a
> good reason for making them non-const?
> (saves me lots of mutable's if they're const;-)

I see two options for making it const, each with its semantic problems:
        1) Just make function::operator() const, but constness won't be passed
through to underlying function objects, so we're dealing with a semantic grey
area.
        2) Have both a const and a non-const version, and pass an "is_const" flag so
that function objects will be invoked as const or non-const and the constness
will be passed through. However, this requires a const version of operator()
to be defined for any function object regardless of whether it is used or not.

I don't really like #1 because it's inheritently unsafe: if one calls a const
function, one expects no modifications. However, mutables, global variables,
and free functions all break this constness guarantee already, so would it be
so bad if constness was lost?

I don't really like #2 because it requires const operator() on functors,
which may not be reasonable for some function objects. Requiring the user to
define extra members that won't/can't be used seems to go against the
intention of C++: don't make the user pay for what s/he doesn't use.

So here's your answer: I dislike both solutions, and could not come up with
another one very easily. I'm leaning toward #1 because guaranteeing constness
is impossible given the ability to link to free functions. Any input would be
very, very helpful, and either solution is reasonably easy to implement.

        Doug Gregor