Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost.Function] detecting ignored arguments?
From: Igor R (boost.lists_at_[hidden])
Date: 2012-06-25 10:15:29


> I have a callback-interface that can optionally process a "ServerError".
> If the callback looks at the error, there should be no default processing
> of the error.
>
> I have created a class callback that accepts two kinds of Boost.Functions:
> one with, one without an error object.
>
> When passing a callback function that does not look at the error,
> the call is ambiguous: both Boost.Functions could accept it.
> Is there a way to work around that problem?
> Can callback detect what kind of function it was passed?
>
>
>
> Some code illustrating my question:
>
>
> #include <boost/function.hpp>
> class ServerError;
>
> template<typename T>
> struct callback
> {
>   typedef boost::function<void(T)> cb_func;
>   cb_func callback_;
>   typedef boost::function<void(T,const ServerError&)> cb_err_func;
>   cb_err_func callback_err_;
>   callback(const cb_func &cb) : callback_(cb) {}
>   callback(const cb_err_func &cb) : callback_err_(cb) {}
> };
>
>
> static void xxx(int) {}
>
> int main (int argc, char **argv)
> {
> // does not compile: ambiguous...
>   callback<int> cb(&xxx);
> // does compile: but I'd like the compiler (or my lib) to automatically pick
> // the right one
>   callback<int> cb(callback<int>::cb_func(&xxx));
>   return 0;
> }

I haven't managed to realise why you get this specific error (maybe
it's some weird MSVC bug), but Boost.Function expects a callable with
the exact signature. Try the following:
  boost::function<void(int)> f1(xxx);
  boost::function<void(int, const ServerError&)> f2(xxx);
and you'll see that the 2nd line does not compile.


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