Boost logo

Boost :

Subject: [boost] [type_erasure] Overloading
From: Christophe Henry (christophe.j.henry_at_[hidden])
Date: 2013-02-12 17:59:25


Hi Steven,

I'm afraid I might need some help again.
I try to take as model the example in the doc
(http://steven_watanabe.users.sourceforge.net/type_erasure/libs/type_erasure/doc/html/boost_typeerasure/concept.html#boost_typeerasure.concept.overload).

First problem, it seems that the example has a typo. It will not
compile without a :Base in the concept_interface specialization:

struct concept_interface< ::foo<T, U,void>, Base, T, Enable> : Base
...

Second, the example overload.cpp includes a as_param.hpp which no more exists.

But my problem is that I need an overload with 1 and 2 parameters:

    Test t;
    any<
        mpl::vector<
            foo<_self, int>,
            foo<_self, int, double>,
            boost::type_erasure::copy_constructible<>
>
> x (t);
    x.foo(1); // calls foo(int)
    x.foo(1,1.0); // calls foo(int, double)

I don't get this to compile, the second foo cannot be found, I suppose
the first one hides the second.

Thanks in advance,

Christophe

Here my failed attempt (well, one of them ;-) ):

template<class T, class U, class V=void, class Enable=void>
struct foo
{};

template<class T, class U, class V>
struct foo<T,U,V,typename boost::disable_if<boost::is_same<V,void> >::type >
{
    static void apply(T& t, const U& u, const V& v) { t.foo(u,v); }
};
template<class T, class U, class V>
struct foo<T,U,V,typename boost::enable_if<boost::is_same<V,void> >::type >
{
    static void apply(T& t, const U& u) { t.foo(u); }
};

namespace boost {
namespace type_erasure {

template<class T, class U, class Base, class Enable>
struct concept_interface< ::foo<T, U,void>, Base, T, Enable> : Base
{
    typedef void _fun_defined;
    void foo(typename rebind_any<Base, const U&>::type arg)
    {
        call(::foo<T, U, void>(), *this, arg);
    }
};

template<class T, class U, class V, class Base>
struct concept_interface< ::foo<T, U,V>, Base, T, typename
Base::_fun_defined> : Base
{
    using Base::foo;
    void foo(typename rebind_any<Base, const U&>::type arg, typename
rebind_any<Base, const V&>::type arg2)
    {
        call(::foo<T, U,V>(), *this, arg,arg2);
    }
};

}
}

struct Test
{
    void foo(int)
    {
        std::cout << "foo(int) called" << std::endl;
    }
    void foo(double)
    {
        std::cout << "foo(double) called" << std::endl;
    }
    void foo(int,double)
    {
        std::cout << "foo(int,double) called" << std::endl;
    }
};

int main()
{
    Test t;
    any<
        mpl::vector<
            foo<_self, int>,
            foo<_self, int, double>,
            boost::type_erasure::copy_constructible<>
>
> x (t);
    x.foo(1); // calls foo(int)
    x.foo(1,1.0); // calls foo(int, double)
    return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk