Boost logo

Boost :

From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2002-02-23 12:14:02


David Abrahams wrote:
> Whoa, that's tricky! How do you extend it to more overloads?

By adding more 'int/long' parameters, see below :). BTW, it seems that I was
too cautious when I added 'foo_impl' wrapper - at least MSVC seems to be
able to handle things basing only on 'int/long' trick.

    #include <iostream>
    #include <typeinfo>

    template< typename T > struct A {};

    namespace aux {

    template< typename T1, typename T2 >
    void foo(A<T1> const&, T2 const&, long, int)
    {
        std::cout
            << "foo(A<" << typeid(T1).name() << "> const&"
            << ", " << typeid(T2).name() << " const&)\n"
            ;
    }

    template< typename T1, typename T2 >
    void foo(T1 const&, A<T2> const&, long, int)
    {
        std::cout
            << "foo(" << typeid(T1).name() << " const&"
            << ", A<" << typeid(T2).name() << "> const&)\n"
            ;
    }

    template< typename T1, typename T2 >
    void foo(A<T1> const&, A<T2> const&, long, long)
    {
        std::cout
            << "foo(A<" << typeid(T1).name() << "> const&"
            << ", A<" << typeid(T2).name() << "> const&)\n"
            ;
    }

    template< typename T1, typename T2 >
    void foo(T1 const&, T2 const&, int, int)
    {
        std::cout
            << "foo(" << typeid(T1).name() << " const&"
            << ", " << typeid(T2).name() << " const&)\n"
            ;
    }

    } // namespace aux

    template< typename T1, typename T2 >
    void foo(T1 const& t1, T2 const& t2)
    {
        aux::foo(t1, t2, 1L, 1L);
    }

    int main()
    {
        foo(int(), int());
        foo(A<int>(), int());
        foo(int(), A<int>());
        foo(A<int>(), A<int>());
        return 0;
    }

Aleksey


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