Re: Reference to void
* James Kanze:
Lucian Radu Teodorescu wrote:
Is the following code valid?
int i = 5;
void* pv = &i;
void& rv = *pv; // Error here on VC 2005
If not, why isn't it valid?
Its not valid for the same reasons that the following is invalid:
void v = 5;
That is to say: a reference is an object and a valid object.
The same can't be said of a pointer, or any pointer.
Let's make the following assumption: every type is implicitly derived
from void. Many C++ programmers can accept that.
Name one. It's manifestly false; it is, in fact, illegal to
derive from void (or from any incomplete type).
Note that Lucian (as I read that article, especially in light of the
rest of it) was describing a future extension, not the current language.
When discussing a future extension of the language it's hardly an
argument against that extension that it doesn't yet exist. ;-)
However, since 'void' has at least two different meanings (sometimes it
behaves like a type, sometimes like a syntactic placeholder) it's IMO a
poor choice for an 'any' type.
Void, as a type, is an incomplete type, which can never be
completed. And in no case can you derive from an incomplete
type. The rules concerning convertion of T* to void* do NOT
obey the rules of derived to base (which only works if the types
are complete), and of course, work even in cases where
inheritance would be illegal, e.g. int* to void*.
Let's think about what the extension would be used for.
There's not much of a technical problem, for
void& rv = *pv;
would simply be translated, internally, to
typeof(pv)& rv = pv;
(except with C++0x mumbo-jumbo-name for typeof) and
void const& r = SomeExpression();
would be translated, internally, to
typeof(SomeExpression) const& r = SomeExpression;
but in both cases with a restriction on what you could do with the
reference, namely the restriction implied by 'void'.
But what's that restriction good for?
Not very much, I think.
And then why not use the keyword 'auto' instead, to get the compiler's
deduced type instead of the restrictive 'void'?
Huh, nice idea, but where can I have seen that before?
Aha! <url: http://std.dkuug.dk/JTC1/SC22/WG21/docs/papers/2003/n1478.pdf>.
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]