Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2002-12-06 07:05:51


David A. Greene wrote:
> [Posted to boost because MPL is not yet released. At what
> point should these questions go to boost-users?]

It's mainly the content of the message that determines whether it should be
posted here or to the Boost Users list; the status of the library - is it in
development, pre-release, or released state - doesn't matter much. Boost
Users is a gateway for, well, Boost users that feel overwhelmed and
intimidated by the high volume and level of technical content that flows
through this list. Basically, it's a place for newbie and frequently asked
questions. If the question is highly technical - as the one I am replying to
- it belongs here.

>
> Say I have a type my_type:
>
> template<typename T, typename U, typename V>
> struct my_type { ... }
>
> Now let's say I want to create a generator that
> binds T to some type but leaves U and V free to
> be filled in later. Basically, I want something
> like this:
>
> template<typename Bound>
> struct my_type_generator {
> template<typename U, typename V>
> struct apply {
> typedef my_type<Bound, U, V> type;
> };
> };
>
> Is there a convenient way to create this with MPL?
> I thought lambda might do the trick,

Yep, that would be intuitive, wouldn't it?

> but apparently not:
>
> typedef my_type<int, mpl::_1, mpl::_2> my_type_generator;
>
> (This will be passed to an MPL algorithm so I didn't
> bother with lambda<>).
>
> This causes a static assertion in mpl::arg<2> where it
> sees a void type for V (I think).

Hmm, actually it should be something like this:

error C2039: 'type' : is not a member of
'boost::mpl::meta_fun3<F>::apply<U1,U2,U3>'

but it doesn't matter much - you are right, it won't compile, currently.

>
> Is the problem that my_type doesn't contain a ::type
> member?

Yes.

> my_type is not a metafunction so maybe it just
> can't be used conveniently with mpl.

Not now. However, I constantly keep finding more and more use cases to be
inclined to provide a built-in library support for this particular
metafunction's form - in particular, so that one could do exactly what
you've tried to:

    typedef my_type<int, mpl::_1, mpl::_2> my_type_generator;

pass it to an algorithm, and it will work as expected.

> It's easy enough
> to manually define my_type_generator, but I think this
> is a pretty common pattern

Very!

> and it might be useful as a library component.

Thanks for the feedback! I would expect the feature to appear in the CVS
somewhere on the weekend.

Aleksey


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