Boost logo

Boost :

Subject: Re: [boost] Efficient tuple implementation
From: Larry Evans (cppljevans_at_[hidden])
Date: 2014-06-13 18:15:52


On 06/06/14 21:18, Louis Dionne wrote:
> Hi,
>
> I recently discovered (or maybe not) a neat trick to implement a tuple-like
> container. The technique has a couple of drawbacks which I will explain later.
> For a real example, you can see my list implementation in Boost.Hana at [1].
> Here's the idea:
>
> auto list = [](auto ...xs) {
> return [=](auto access) { return access(xs...); };
> };
>
> auto head = [](auto xs) {
> return xs([](auto first, auto ...rest) { return first; });
> };
>
> auto tail = [](auto xs) {
> return xs([](auto first, auto ...rest) { return list(rest...); });
> };
>
> auto length = [](auto xs) {
> return xs([](auto ...z) { return sizeof...(z); });
> };
>
> // etc...
> // then use it like
>
> auto three = length(list(1, '2', "3"));
>

IIRC, list uses an evaluator and "tagged" lists where the 1st element in
the list is a tag for the operation to be performed and the
evaluator dispatches on this tag to do the operation with the
operands being the remaining args in the list.

The attached is my first attempt at doing something similar with the
idea you propose.

I'm wondering if spirit might use something similar where the
tags, instead of being op_or or op_and, would be op_alt and op_seq
where op_alt would correspond to the alternative parser:

http://www.boost.org/doc/libs/1_55_0/libs/spirit/doc/html/spirit/qi/reference/operator/alternative.html

and op_seq would correspond to the sequence parser:

http://www.boost.org/doc/libs/1_55_0/libs/spirit/doc/html/spirit/qi/reference/operator/sequence.html

Does this look useful? Any suggested improvements would be appreciated.

-regards,
Larry




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