Boost logo

Boost Users :

Subject: Re: [Boost-users] [Function] requiring an exact match in function signature?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2010-04-03 09:12:26


Nathan Ridge a écrit :
> Hello,
>
> I'm wondering if there is a way to force boost::function to only accept
> functions/functors with the exact same signature.
>
> I need this because there are cases in my program where passing a function
> with a different but convertible signature is almost certainly an error,
> and it would really be nice if such uses triggered a compiler error.
>
> For instance, boost::function will readily convert a mutator function
> with this signature:

boost::function doesn't really "convert" anything. The
boost::function<sig> objects simply define an operator() with the `sig'
signature, which in turns calls the wrapped function object by means of
a virtual function call.

So if your function is callable from a `sig' context, it works even
though that's not its exact signature, because the arguments specified
are convertible to the real arguments and the real return type is
convertible to the specified return type.
It doesn't really make sense to worry about the exact signature in my
opinion;

Anyway, if you want to test the signature of a function object, just
take the address of &T::operator() and check it is what you want. Of
course that won't work if it's a template or if it is overloaded.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net