Topic: Postfix overloading


Author: lars_skiba@web.de (Lars Skiba)
Date: Sun, 26 Jan 2003 12:15:21 +0000 (UTC)
Raw View
In nearly every case the implementation of the operator++(int) and
operator--(int) looks like that:
class Example {
   public:
     Example& operator++() {// or operator--
       //implementation
       return(*this);
     }
     const Example operator++(int) { // or operator--
       Example temp(*this);
       ++*this;
       return temp;
     }
}
Whats the sence of the possibility to overload the postfix operator? Why
is the postfix operator not auto-implemented? That would has the
advantage that compiler could optimize postfix-uses (even it is a
mistake of the programmer, that he used the postfix one).
--
www.c-plusplus.de
The german C++ Forum

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: danielgutson@hotmail.com (danielgutson@hotmail.com)
Date: Sun, 26 Jan 2003 21:58:58 +0000 (UTC)
Raw View
lars_skiba@web.de (Lars Skiba) wrote in message news:<b10dnl$of3$04$1@news.t-online.com>...
[...]
> is the postfix operator not auto-implemented? That would has the
> advantage that compiler could optimize postfix-uses (even it is a
> mistake of the programmer, that he used the postfix one).

One could say something similar about operator + and operator += :
class Example
{
public:
   Example& operator += (const Example& other)
   {  // implementation
      return *this;
   }

   Example operator + (const Example& other) const
   {
      Example temp(*this);
      temp+=other;
      return temp;
   }
};

and perhaps more examples; if you allow me, let me generalize your
proposition by using some directive to let the compiler know : "I want
this method: auto-implement it", for example with the already existent
'auto' keyword.

An 'auto-implementation' table should be standarized (i.e. saying:
method    auto-implemented using
------    ----------------------------
++(int)   ++()
+         +=
...       ...

and a standard auto-implementation for each one (left column).

Your example would look like:

class Example
{
public:
   Example& operator += (const Example& other)
   {  // implementation
      return *this;
   }

   auto Example operator + (const Example& other) const;

   Example& operator ++ ()
   {  /* your example */ }

   auto Example operator ++(int);
};

I think this is important in terms of optimization and readability (as
far as the client 'see' that some operator is auto-implemented).

 Daniel.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: lars_skiba@web.de (Lars Skiba)
Date: Mon, 27 Jan 2003 22:56:42 +0000 (UTC)
Raw View
> I think this is important in terms of optimization and readability
I totally agree!
This is...
type var = a + b + c;
...more readable then this:
type var = a;
var += b;
var += c;
But the first one is slower. A keyword like auto would remove this
problem. Or sth. like that:
template<class T>
bool check(T& first, T& second, T& third)
{
   T tmp1 = first + second + third; //the same with -
   T tmp2 = first; tmp2 += second; tmp2 += third; // the same with -=
   return tmp1==tmp2;
}
The standard should say sth like that to this example: For each type
witch has a operator+(-) or operator += (-=) check must return true.
And the same thing with postfix/prefix. This would improve the
perfomance *and* readability of C++.


--
www.c-plusplus.de
Das deutsche C++ Forum

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]