Boost logo

Boost :

From: Greg Colvin (Gregory.Colvin_at_[hidden])
Date: 2003-01-04 15:22:24


At 12:56 PM 1/4/2003, Aleksey Gurtovoy wrote:
>Greg Colvin wrote:
>> > If it was run-time C++, I would be happy with 'apply_tuple',
>> > but in MPL domain "tuple" isn't really the right word, and I
>> > don't like 'apply_seq' or, worse yet, 'apply_sequence'. Or
>> > should it be 'seq_apply' (from an English language standpoint)?
>>
>> If this construct applies a metafuntion to a sequence
>
>It does and it doesn't :). Sorry if I wasn't clear about the semantics; it
>does not apply a metafunction to every element of a sequence;

That would be for_each ?

>instead, it unrolls the sequence and passes all its elements to the
>metafunction as separate arguments, all at once.
>
>To clarify it further, here's how a run-time equivalent of that hypothetical
>'apply_tuple' could look like:
>
> template< typename F, typename Tuple >
> typename result_type<F>::type
> do_apply(F f, Tuple const& args, arity<1>)
> {
> return f(get<0>(args));
> }
>
> template< typename F, typename Tuple >
> typename result_type<F>::type
> do_apply(F f, Tuple const& args, arity<2>)
> {
> return f(get<0>(args), get<1>(args));
> }
>
> // ...
>
> template< typename F, typename Tuple >
> typename result_type<F>::type
> apply(F f, Tuple const& args)
> {
> enum { n = tuple_length<Tuple>::value };
> return do_apply(f, args, arity<n>());
> }
>
> void f(int, char const*);
>
> int main()
> {
> apply(f, make_tuple(5, "text")); // here
> }
>
>
>
>> then "apply_to_sequence" would be an accurate name.
>
>Thanks for clarifying the language side. Would it be still accurate for the
>aforementioned semantics?

Yes, if this is in some sense the "natural" thing to do.

>> Too bad it can't just be "apply".
>
>Yeah, unfortunately it can't be. You have to have different notation for
>invoking a function with a sequence of elements, 'cause just determining if
>the first and the only argument is a sequence and unrolling it is not enough
>- a (meta)function itself might expect exactly the original sequence, after
>all.

Yep. Is there no way to use function call syntax for the 0...n args case?


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