Re: Reference to void
Joshua Lehrer wrote:
This has been a pet peeve of mine for a while, probably pretty high up
on my C++ wish list.
(ignoring cv-qualifications for simplicity)
If T* can degrade to void*, why can't T& degrade to void&?
Because void is an incomplete type.
The following is legal, and the basis of how ScopeGuard works:
const Base & b = make_sub_class(params);
The unnamed temporary is bound to the const-reference and is guaranteed
to be alive for the lifetime of 'b'.
It also only works if Base is a complete type.
I sometimes find it a pain to make my implementations inherit from a
common base just so that I can bind a const-reference to the temporary,
as above. Why can't I write:
const void & v = make_sub_class(params);
or
const void & v = std::pair<Lock,Lock>(mx1,mx2);
In my mind, it makes sense. All types should be considered to inherit
(virtually) from void.
What you're looking for isn't void, it's Object. In Java.
Personally, I've never missed it in C++.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]