Topic: operator void()
Author: haberg@REMOVE.matematik.su.se (Hans Aberg)
Date: 1999/05/10 Raw View
It appears legal in C++, and my compiler (CW Pro 4) accepts it, to have a
class with operator void(). That is
class C {
public:
operator void();
};
(Please do not confuse this with C::operator void*().)
Is this correct according to the C++ standard, and what is then the
current use of it?
If it has no current use in C++, I can indicate one:
If one can put a label on a function so that it must be used in a context
where its return value is taken care of, then the operator void() could be
used to trigger an evaluation in the situation where the return value is
not taken care of. For example, suppose one has the following
must_return C f() { ... }
which would indicate that the following use of f() is legal
C c = f();
but the following is not
f(); // Value C is not taken care of.
Then the presence of C::operator void() would allow this conversion from C
to void, making it valid again.
The use of such a construction is when one has special "return"
optimizations which should not be used in other contexts (where ther is no
explicit "return" statement). Instead having the programmer using the
interface write special constructs for the "return", one can then instead
hide away it.
Hans Aberg * Anti-spam: Remove "REMOVE." from email address.
* Email: Hans Aberg <haberg@REMOVE.member.ams.org>
* Home Page: <http://www.matematik.su.se/~haberg/>
* AMS member listing: <http://www.ams.org/cml/>
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/05/10 Raw View
Hans Aberg wrote:
>
> It appears legal in C++, and my compiler (CW Pro 4) accepts it, to have a
> class with operator void(). That is
> class C {
> public:
> operator void();
> };
> (Please do not confuse this with C::operator void*().)
>
> Is this correct according to the C++ standard, and what is then the
> current use of it?
>
The standard doesn't seem to prohibit it. And, there is a note about
such conversion operators in 12.3.2/1:
**********
A conversion operator is never used to convert a (possibly cv qualified)
object to the (possibly cv qualified) same object type (or a reference
to it), to a (possibly cv qualified) base class of that type (or a
reference to it), or to (possibly cv qualified) void. (101)
(101) Even though never directly called to perform a conversion, such
conversion operators can be declared and can potentially be reached
through a call to a virtual conversion operator in a base class.
***********
That said, I don't understand what is the use of it in a real-world
program.
--
Biju Thomas
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]