Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2006-05-11 19:57:16


Joel de Guzman wrote:
> Eric Niebler wrote:

>> - how is is_view<> used? The Extensibility section says I have to
>> specialize it, but I don't think it says anywhere why.
>
> I'll discuss this in another post.

I want to discuss this separately because I am re-thinking this.
The issue, again, is how a view holds a sequence. Holding it
by reference is the sensible answer. However, views are typically
quite small and can be passed on as values. That way, we don't
have the issue of dangling references. For example, for complex
views, you'd want to construct them piecemeal using local variables.
For example, here's the "insert" function:

     typedef result_of::insert<
         Sequence const, Position, T>
     result_of;
     typedef typename result_of::left_type left_type;
     typedef typename result_of::right_type right_type;
     typedef typename result_of::single_view single_view;
     typedef typename result_of::left_insert_type left_insert_type;
     typedef typename result_of::type result;

     left_type left(
         fusion::begin(seq), convert_iterator<Position>::call(pos));
     right_type right(
         convert_iterator<Position>::call(pos), fusion::end(seq));
     single_view insert(x);
     left_insert_type left_insert(left, insert);
     return result(left_insert, right);

This is only possible if left, right, insert and left_insert, holds
its views *by value*. Otherwise, we'll have dangling references
pointing to stale objects in the stack that are dead after we exit
the function.

So, as a matter of convenience, views in Fusion-2 are held by value.
Yet, again, I am not quite sure if this is a good thing or not.
It requires us to differentiate between a view and a container.
Containers are always held by reference.

Regards,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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