Is std::tr1::function intended to work ONLY with function objects which implement the operator() as const?
Hi,
I'm using an STL implementation that fails to compile the below code.
So the question is: is the below code standard? Or the behavior is
implementation-specific?
------------------------------------------------------------------------------
#include <functional>
struct functor
{
void operator() (int) { } // not const
};
int main()
{
functor fun;
std::tr1::function<void(int)> f = fun; // Is this standard?
return 0;
}
------------------------------------------------------------------------------
My understanding is that the code is standard compliant. I have looked
into TR1 draft and function::operator() is defined as const - which
leads to the impression that function::operator() must not modify any
internal state of function. Conceptually function::operator() can be
thought as const - forwarding a call to an external entity should not
change the state of [std::tr1::function] instance. So that I find the
const restriction imposed by the standard on function::operator()
reasonable. But I don't find reasonable at all to conclude from this
that when the target callable object is a function object (as in the
above example), the function object must implement operator() as
const! This eliminates the opportunity to use conveniently functors
which modify their state in operator(), which it turn eliminates the
generality of std::tr1::function.
I haven't found yet in the draft any reference regarding the intent to
limit the usage of [std::tr1::function] to calling ONLY function
objects with CONST operator(). I suppose the STL implementation could
use mutable on the internal member(s) maintained by
[std::tr1::function] allowing calls to the non-const operator() of the
target object, while keeping the logical const-ness of
std::tr1::function::operator().
Well, this is my (current) interpretation of TR1 draft regarding
std::tr1::function. At least I didn't find yet a reference in the
standard to contradict it. Please let me know if anyone has more
details of the intent of the standard committee regarding the
behaviour of std::tr1::function in conjunction with function objects.
The above code compiles fine with Boost::Function.
Regards,
Florin Neamtu
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]