Boost logo

Boost :

From: Daniel Walker (daniel.j.walker_at_[hidden])
Date: 2007-03-20 09:51:37


Hello,

I would like a Metafunction similar to the one created by
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF but that would detect nested
template members rather than nested typedefs and classes. It would
allow you to do something like the following:

#include <utility>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/has_template_xxx.hpp>
#include <boost/utility/result_of.hpp>

// Create has_template_result to detect nested members like
// template<class T> struct result {}.
// The first macro argument is the member name and the second argument
// is the number of template parameters.
BOOST_MPL_HAS_TEMPLATE_XXX_TRAIT_DEF(result, 1)

struct pair_maker {
    template<typename F> struct result;
    template<typename F, typename Arg0, typename Arg1>
    struct result<F(Arg0, Arg1)> {
        typedef std::pair<Arg0, Arg1> type;
    };

    template<typename Arg0, typename Arg1>
    typename result<pair_maker(Arg0, Arg1)>::type
    operator()(Arg0 a0, Arg1 a1) const
    {
        return std::make_pair(a0, a1);
    }
};

int main()
{
    using namespace boost;

    typedef char value_type;
    BOOST_MPL_ASSERT((
        has_template_result<
            pair_maker,
            pair_maker(value_type, value_type)
>
    ));

    typedef result_of<
        pair_maker(value_type, value_type)
>::type result_type;

    value_type x, y;
    pair_maker f;
    result_type result = f(x, y);
}

The attached patch implements this. Apply with 'patch -p0 <
djw_has_template.patch' from the boost root directory. It's based on
the implementation of BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF and works
basically the same way. The patch creates two files
boost/mpl/has_template_xxx.hpp and
boost/mpl/aux_/config/has_template_xxx.hpp. Similarly to has_xxx, when
the maco BOOST_MPL_CFG_NO_HAS_TEMPLATE_XXX is defined the
has_template_xxx::value is always false or a user supplied default. At
this time, I'm unable to write all the various compiler workarounds
that boost/mpl/has_xxx.hpp includes. BOOST_MPL_CFG_NO_HAS_TEMPLATE_XXX
is defined for every workaround mentioned in boost/mpl/has_xxx.hpp.
I've tested it with gcc 3.3, 3.4, 4.0, and 4.1 and it seems to work
fine. If there's any interest, let me know and I'll supply
documentation and tests.

Thanks!
Daniel Walker




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