should boost::ref forward some operators?

I ran into the following problem. I have an algorithm which looks like: template<typename in_t, typename out_t> void Alg (in_t in, in_t inend, out_t out) { for (; in != inend; in++, out++) do_something() } Now if I want to call this algorithm more than once, it would be convenient to pass out by ref, so it would be incremented: Alg (v.begin(), v.end(), boost::ref (out)) that is, pass a reference to some kind of output iterator this fails, because boost::ref doesn't have operator++ defined for reference_wrapper. My question is, should boost::ref forward operators like ++ so that this would work?

Neal D. Becker wrote:
I ran into the following problem. I have an algorithm which looks like:
template<typename in_t, typename out_t> void Alg (in_t in, in_t inend, out_t out) { for (; in != inend; in++, out++) do_something() }
Now if I want to call this algorithm more than once, it would be convenient to pass out by ref, so it would be incremented:
Alg (v.begin(), v.end(), boost::ref (out)) that is, pass a reference to some kind of output iterator
this fails, because boost::ref doesn't have operator++ defined for reference_wrapper.
My question is, should boost::ref forward operators like ++ so that this would work?
To make it work you can use the 'unwrap' function template just like it is in "boost/bind.hpp" and "boost/mpl/aux_/unwrap.hpp". You algorithm would look like this: template<typename in_t, typename out_t> void Alg (in_t in, in_t inend, out_t out) { for (; in != inend; in++, unwrap(out)++) do_something() } -- Maxim Yegorushkin MetaCommunications Engineering http://www.meta-comm.com/engineering/

Neal D. Becker wrote:
I ran into the following problem. I have an algorithm which looks like:
template<typename in_t, typename out_t> void Alg (in_t in, in_t inend, out_t out) { for (; in != inend; in++, out++) do_something() }
Now if I want to call this algorithm more than once, it would be convenient to pass out by ref, so it would be incremented:
Alg (v.begin(), v.end(), boost::ref (out)) that is, pass a reference to some kind of output iterator
this fails, because boost::ref doesn't have operator++ defined for reference_wrapper.
My question is, should boost::ref forward operators like ++ so that this would work?
Altough I kindof see your point, I still feel your key problem is that your algorithm "should" return the used iterator? (Ala functional/STL style). template<typename in_t, typename out_t> out_t Alg (in_t in, in_t inend, out_t out) { for (; in != inend; in++, out++) do_something(); return out; } Regards // Fredrik Blomqvist

At Tuesday 2004-02-03 14:47, you wrote:
Neal D. Becker wrote:
I ran into the following problem. I have an algorithm which looks like:
template<typename in_t, typename out_t> void Alg (in_t in, in_t inend, out_t out) { for (; in != inend; in++, out++) do_something() }
Now if I want to call this algorithm more than once, it would be convenient to pass out by ref, so it would be incremented:
Alg (v.begin(), v.end(), boost::ref (out)) that is, pass a reference to some kind of output iterator
this fails, because boost::ref doesn't have operator++ defined for reference_wrapper.
My question is, should boost::ref forward operators like ++ so that this would work?
Altough I kindof see your point, I still feel your key problem is that your algorithm "should" return the used iterator? (Ala functional/STL style).
template<typename in_t, typename out_t> out_t Alg (in_t in, in_t inend, out_t out) { for (; in != inend; in++, out++) do_something(); return out; }
perhaps also ++in, ++out since they're never slower and often faster btw, what's the difference between this and std::transform()? (unless it's the constant do_something();
Regards // Fredrik Blomqvist
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"
participants (4)
-
Fredrik Blomqvist
-
Maxim Yegorushkin
-
Neal D. Becker
-
Victor A. Wagner, Jr.