Francis Glassborow wrote:
post fix requires a copy unless you are asking that in the
free-standing case the compiler treats them identically.
It is commonly believed that the postfix operator SHOULD make a copy,
but should it really? It can also return a cheap proxy that increments
the subject upon destruction, in case the subject is expensive to
copy.
class big
{
// ...
class proxy
{
big* const subject;
public:
explicit proxy(big* p) : subject(p) { }
~proxy() { ++*subject; }
operator big&() const { return *subject; }
// to be complete and allow ++*i++, (i++)->foo(), etc.
big::pointer operator->() const { return &**subject; }
big::reference operator*() const { return **subject; }
};
big& operator++() { /* the usual prefix increment */ }
proxy operator++(int) { return proxy(this); }
};
I tested this on a "big" object whose postfix had been 15 times slower
than prefix before, and observed no statistically significant
difference
in the speed between the two.
Maybe this could be a standard idiom for implementing postfix
operators? :)
matching templates.
[ comp.lang.c++.moderated. First time posters: Do this! ]