From: Lucian Radu Teodorescu on 25 Oct 2006 08:54 { 8.3.2/1 prohibits declarations of references to void. -mod } Hello 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? Best regards -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Greg Herlihy on 25 Oct 2006 11:37 Lucian Radu Teodorescu wrote: > { 8.3.2/1 prohibits declarations of references to void. -mod } > > Hello > > 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? For the same reason it is not legal to declare a variable with a void type or to dereference a void pointer - "void" in C and C++ denotes either that the type is not known (as in the case of a void pointer) or there is no type at all (as in the case of a function returning void). So whether the type that a void declaration represents is unknown or absent, it should be clear that allocating, assigning or evaluating a "void" type (or a reference to one) would either have unpredictable consequences or not even be possible in the first place. In other words: since a C++ program could not do anything useful with a reference to void, the C++ language is better off without such a concept. Greg -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Salt_Peter on 25 Oct 2006 17:57 Lucian Radu Teodorescu wrote: > { 8.3.2/1 prohibits declarations of references to void. -mod } > > Hello > > 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. Consider: int i = 5; What exactly is i ? Is it not an alias to what would otherwise have been an anonymous variable in memory? Granted a reference is not the same beast, but it does serve the same purpose. > > Best regards > -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Joshua Lehrer on 25 Oct 2006 22:55 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&? 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'. 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. As pointers to subclasses can be cast to pointers to bases, so can pointers to anything therefore be converted to pointers to void. Likewise, references to subclasses can be cast to references to bases, therefore references to anything should be convertible to references to void. -joshua lehrer http://www.lehrerfamily.com/ -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jiang on 26 Oct 2006 06:29
On Oct 26, 11:55 am, "Joshua Lehrer" <usenet_...(a)lehrerfamily.com> 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&? > If I understand this issue correctly, a reference is an alias for an *object*. However, type void is an imcomplete type and we do not have an object with type void. If we do not have an underlying object, what is the point for a void&? Even suppose we have cv-void&, it is not clear for me that how can I use it in my expressions. As we know the expresion evalution will use the real object, which the reference refers. > 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'. > > 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); > Ok, now you have a imcomplete referece v, then what will do next? > 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. So you want to inherite all your class from a incomplete type? Or, you believe that "everything is just void" ? :-) > As pointers to subclasses can be cast to > pointers to bases, so can pointers to anything therefore be converted > to pointers to void. It is well-defined behavior to cast your pointer to void* in current C++. > Likewise, references to subclasses can be cast to > references to bases, therefore references to anything should be > convertible to references to void. > May I change the word "anything" to "nothing" here? Please note type void and type void* are different. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |