From: Marco Costalba (mcostalba_at_[hidden])
Date: 2007-10-07 04:41:52


On 10/7/07, Joel de Guzman <joel_at_[hidden]> wrote:
>
> > Using operator<<() would be nice, but if this can create confusion it
> > can be changed to something else 'Surely better'.
>
> I don't quite like the operator in this case, FWIW. As Dave (A)
> always says, use of such operators should be idiomatic. In this
> case, it is not.
>

Ok, I have removed operator<<(), now test example is:

int main()
{
    typedef boost::tuple<
        int(char)
      , double(int, char)
      , char(std::string)
      , void(std::string, std::string, std::string)
      , int()
>
    Signatures;

    boost::overload<Signatures> f;

    // assign functions in any order
    f.add_overload(foo4);
    f.add_overload(foo2);
    f.add_overload(foo1);
    f.add_overload(foo5);
    f.add_overload(foo3);

    int i = f('x');
    double d = f(123, 'x');
    char c = f("hello");
    int k = f();
    f("Hello", ", World", ", I'm Joel");

    BOOST_ASSERT(i == 123);
    BOOST_ASSERT(d > 123.455 && d < 123.457);
    BOOST_ASSERT(c == 'x');
    BOOST_ASSERT(k == 7);

    return boost::report_errors();
}

BTW I have also changed the mechanism to assign the functions, instead
of operator=() overloads now I use a metafunction to find the type,
see

http://digilander.libero.it/mcostalba/boost_overload/overload.hpp

I don't have fixed the problem spotted by Joel, in that it still fails
if a functor instead of a function pointer is passed to
add_overload(), but I would think this new tecnique could be more
extensible the the former.

In particular the core type matching is now:

boost::is_same<F, T>::value

where F is the type of the function pointer to store and T is the type
of the n-th element of the 'Signatures' tuple.

I would think to fix completely the problem reported by Joel we could
write instead:

boost::is_convertible<F, boost::function<T> >

But for unknown reasons this fails! All types of function pointers match!

Could someone please be so kind to enlight me?

Thanks
Marco