[C++-sig] Re: how to wrap a template function without casting

David Abrahams dave at boost-consulting.com
Wed Sep 29 19:01:21 CEST 2004


"Fred Houben" <qhf at oce.nl> writes:

> BOOST_PYTHON_MODULE(res)
>
> {
>
>             class_<Resource>("Resource")
>
>                         .def ("hasResource", (void (Resource::*)(std::string&)) &Resource::hasResource)  
>
>             ;
>
> }
>
>  
>
> The problem is that hasResource has to be able to take about 50 different formal parameters. I don't want to cast all of them. Is it possible to wrap hasResource without having to cast every signature?

Fred, your mailer is very impolite, with the extra line breaks in
code, and none in regular text.

All I can suggest is a metaprogramming solution, something like:

    // untested
    #include <boost/mpl/vector50.hpp>
    #include <boost/mpl/for_each.hpp>
    #include <boost/type.hpp>
    
    using namespace mpl::placeholders;

    // A function object that does the def you want for any given type
    struct def_hasResource
    {
        def_hasResource(class<Resource>& c) : c(c) {}

        template <class T>
        void operator()(boost::type<T>) const
        {
            void (Resource::* pmf)(T) = &Resource::hasResource;
            c.def("hasResource", pmf);
        }

        class<Resource>& c;
    };

    ...

        typedef boost::mpl::vector48<           // if you have 48 types
                std::string&, int, double, ... 
        > param_types;

        class_<Resource> c("Resource");
        boost::mpl::for_each<param_types, boost::type<_> >(hasResource(c));

HTH,
-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list