Boost logo

Boost :

From: Martin Weiser (weiser_at_[hidden])
Date: 2007-06-11 08:32:02


Dear all,

while I appreciate boost::fusion very much, I've recently stumbled across
a point that I don't understand. It appears to me that

- const is transitive, i.e. dereferencing an iterator into a const vector
yields a reference to a const element and

- transformation algorithms work on const sequences.

Combined, these two imply that an in-place modification of a sequence
through a view generated by a transformation algorithm is impossible.
While this is perfectly reasonable for transform, it seems quite
restrictive for filter or erase. After all, filter_view accepts a
non-const input sequence.

My point is probably best illustrated by the following code snippet:

#include <iostream>
#include <boost/type_traits/is_same.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/algorithm/transformation/filter.hpp>
#include <boost/fusion/sequence/container/vector.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/sequence/view/filter_view.hpp>

struct SetToZero
{
  template <typename X>
  void operator()(X& x) const
  {
    x = 0;
  }
};

int main(void)
{
  using namespace boost;
  using namespace boost::fusion;

  typedef vector<int,char,double,int> Vec;
  Vec vec(3,'.',1.0,4);
  std::cout << vec << '\n';

  // This does not compile (error: assignment of read-only reference »x«)
  for_each(filter<int>(vec),SetToZero());

  // This works...
  for_each(filter_view<Vec,is_same<mpl::_,int> >(vec),
           SetToZero());
  std::cout << vec << '\n';
  
  
  return 0;
}

So I'm wondering if this is intended? If yes, how do I best modify (part
of) sequences defined by transformation algorithms?

Thanks,
Martin

-- 
Dr. Martin Weiser                  web:     www.zib.de/weiser
Zuse Institute Berlin              mail:    weiser_at_[hidden]
Numerical Analysis and Modelling   pgp key: www.zib.de/weiser/weiser.asc



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