Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Function] detecting ignored arguments?
From: Christoph Duelli (duelli_at_[hidden])
Date: 2012-06-25 07:21:39


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

In the meantime I have found a possible workaround:
If I change
   typedef boost::function<void(T,const ServerError&)> cb_err_func;
to
   typedef boost::function<void(const ServerError&,T)> cb_err_func;
the code does compile.

Best regards and thank you for looking into this
Christoph


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