Boost logo

Boost Users :

From: Raider (sraider_at_[hidden])
Date: 2007-07-23 17:29:26


> Better, but you have proxied a non-const vector, and you're presenting a
> const interface. Try this:
>
> class vec
> {
> public:
> vec(sv& v = global) : v_(v) {}
> typedef sv::iterator iterator;
> typedef sv::iterator const_iterator;
> typedef sv::const_iterator const_iterator;
> iterator begin() const { return v_.begin(); }
> iterator end() const { return v_.end(); }
> void push_back(const string& s) const { v_.push_back(s); }
> private:
> sv& v_;
> };
>
> The const-ness of the proxy itself shouldn't matter. Only the const-ness
> of the proxied object should matter.
>

I want const_iterator to be dereferencable to const object and iterator
to non-cont object. Like this:

class vec
{
public:
     vec(sv& v = global) : v_(v) {}
     typedef sv::iterator iterator;
     typedef sv::const_iterator const_iterator;
     iterator begin() const { return v_.begin(); }
     iterator end() const { return v_.end(); }
     void push_back(const string& s) const { v_.push_back(s); }
private:
     sv& v_;
};

This allows me make no difference does GetVector() return `vec' type
(proxy) or `sv&' (reference to the real container):

for (xxx::iterator it(GetVector().begin(), end(GetVector().end());
     it != end; ++it) { modify(*it); }

for (xxx::const_iterator it(GetVector().begin(), end(GetVector().end());
     it != end; ++it)
{
   /*
   here I'm SURE that (*it) is READ-ONLY:
   this knowlege make my code safer
   because comiler will detect mutations and disallow them
   */
}

I want to use BOOST_FOREACH instead of `for' statements above to:
1. don't care that `xxx' type is
2. call GetVector() only once

Now I found some unsteady way:
#define BOOST_FOREACH_NO_RVALUE_DETECTION
makes my code compile. I'll try to dig in this direction.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net