| Christopher Smith on 14 Oct 2003 15:26:31 -0000 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: [ALACPP] where the boost meets the stl |
On Tue, 2003-10-14 at 08:01, Jon Stewart wrote:
> > > Yeah, I went to sleep thinking the only hook would be "operator int",
> > > but I thought that couldn't possibly be what you meant. ;-)
>
> It's cool, though, because it comes with the realization that one can use
> type conversion to let an object know when it's being used as an rvalue
> and take appropriate action, thus enabling a form of lazy evaluation.
My goodness! It's almost like you were doing our reading and paid
attention to the section on auto_ptr! ;-)
> > FYI, there is an even more evil way to do it that I also thought about:
> >
> > Create a wrapper iterator and dereferenced type that did all the
> > evilness in operator()=(x). See, I can top your evil foo!
>
> so... like this?
>
> fill_n(MakeEvilIterator(back_inserter(vec)), 20, var(i)++);
>
> // EvilIterator overloads operator()=(x) to take the lambda function
> // and assign the result of a call to the back_inserter.
>
> One issue, I'm not sure it's a problem, is that you sort of have the evil
> iterator and the lambda function meeting in the middle, so to speak. For
> lack of vocabulary, they "converge".
Oh, I was thinking far worse than this. I was moving the lambda stuff
into a type which wrapped the back_inserter and did operator()=(x) by
doing i++. This is necessary because the evil iterator is going to be
derefed before operator()=(x) is invoked. So, you have something like
this, dropping the use of lambda for clarity (forgive any obvious
syntactical errors.. it's early):
template<typename I> class EvilIterator {};
template<typename I>
class EvilType {
public:
EvilType(I anIter, int& aCounter)
: iter(anIter), counter(aCounter) {}
EvilType& operator=(int aNumber) {
*iter = aNumber + (counter++);
}
protected:
void advanceIter() {
++iter;
}
private:
I iter;
int& counter;
friend class EvilIterator<I>;
}
template<typename I>
class EvilIterator {
public:
explicit EvilIterator(const EvilType<I>& aCoConspirator)
: coConspirator(aCoConspirator) {}
EvilIterator<I>& operator++() {
coConspirator.advanceIter();
return *this;
}
EvilType<I>& operator*() {
return coConsiprator;
}
private:
EvilType<I> coConspirator;
};
fill_n(EvilIterator(EvilType(back_inserter(vec), i)), 20, 0);
> (If Flannery O'Connor had been a l33t C++ h8x0r, she'd write code like
> this.)
That's reason enough not to do it.
I may never forgive you for making me think about this stuff.
--
Christopher Smith <x@xxxxxxxx>
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp