Topic: type void&
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/26 Raw View
Biju Thomas wrote:
>
> David R Tribble wrote:
> >
> > Is type 'void&' (reference to void) illegal? I can't find it
> > explicitly disallowed in 8.3.2 or 8.5.3, so perhaps someone can tell
> > me if it is legal or not.
>
> Let's assume it is legal. Then, how will you initialize it?
Probably by dereferencing a void* or by direct binding:
int i;
void& r = i;
Where is the problem ?
--
Valentin Bonnard
[ 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: bill@gibbons.org (Bill Gibbons)
Date: 1999/05/27 Raw View
In article <7hiatb$aev$1@engnews1.eng.sun.com>, clamage@eng.sun.com (Steve
Clamage) wrote:
> Type void is an incomplete type which cannot be completed.
> There are no objects of type void, so it doesn't strictly
> make sense to have a reference to void.
>
> You could make basically these same arguments about void*,
> but it is useful to have a "generic pointer". Type void*
> is therefore a special case meaning "pointer to an object
> of unknown type", which (like any pointer) could also be null.
>
> You can pass around by value or otherwise assign void pointers
> with no conceptual difficulty. Not so with references.
>
> You could use a void& to initialize another void&, but that's
> about all. You couldn't use a void& in any other expression
> context, because use of a reference means the object to which it
> refers -- and that there isn't one for void&. To use a void&,
> you almost always need to take the address of the void&,
> negating most of the advantages.
>
> I don't see that void& would provide any additional expressive
> power or benefits, given the properties of void in C++.
However, note 5.3.1 paragraph 1:
The unary * operator performs indirection: the expression to which it is
applied shall be a pointer to an object type, or a pointer to a function
type and the result is an lvalue referring to the object or function to
which the expression points. If the type of the expression is
"pointer to T," the type of the result is "T." [Note: a pointer to an
incomplete type (other than cv void ) can be dereferenced. The lvalue thus
obtained can be used in limited ways (to initialize a reference, for
example); this lvalue must not be converted to an rvalue, see 4.1. ]
And 4.1 paragraph 1:
An lvalue (3.10) of a non-function, non-array type T can be converted to
an rvalue. If T is an incomplete type, a program that necessitates this
conversion is ill-formed.
There are certainly cases where it is useful to deal with an lvalue of
incomplete type; that's why this language is in the standard.
"void" is sort of an extremely incomplete type. It may be that "void&"
is useful for the same reasons that lvalues of other incomplete types
are useful. It may be particularly useful in templates, where any
asymmetries in the type system tend to make otherwise reasonable
programs ill-formed.
-- Bill Gibbons
bill@gibbons.org
---
[ 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: deepblack@geocities.com (Luis Coelho)
Date: 1999/05/27 Raw View
On 26 May 99 02:45:30 GMT, Barry Margolin <barmar@bbnplanet.com>
uttered the following words:
>This particular example could be written better using templates, but
>perhaps there are examples that couldn't. For instance, templates tend to
>result in duplicate code for each instantiation, so if this function had
>lots of code that wasn't dependent on the type of thing, it would be more
>efficient in code space to use a single function with a void& parameter
>rather than a templated function.
It still can be done with void*. As I pointed out earlier, if you
don't want to burden your users with a pointer deference, yuo can
write a wrapper.
<code>
void real_f(void*);
template <typename T> inline
void f(T& ref)
{
real_f(&ref);
}
</code>
I don't think anyone one has pointed anything which could be done
with a void& that can't be done now. Is it really worth it
burdening the language even more with details and further
overload keyword void, just to get a little syntatic sugar?
regards,
Luis Coelho.
C++ Programming Language, 3rd Ed. by B. Stroustrup. My exercise answers at:
http://www.geocities.com/SiliconValley/Way/3972/index.html
---
[ 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: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/28 Raw View
Luis Coelho wrote:
> I don't think anyone one has pointed anything which could be done
> with a void& that can't be done now. Is it really worth it
> burdening the language even more with details and further
^^^^^^^^^
> overload keyword void, just to get a little syntatic sugar?
But there is no burden. No additionnal meaning: this meaning
is _already_ there. vodi is already overloaded to mean three
completly different things; is change could perhaps make things
more regular.
--
Valentin Bonnard
---
[ 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: David R Tribble <dtribble@technologist.com>
Date: 1999/05/24 Raw View
Biju Thomas wrote:
>
> David R Tribble wrote:
>>
>> Is type 'void&' (reference to void) illegal? I can't find it
>> explicitly disallowed in 8.3.2 or 8.5.3, so perhaps someone can tell
>> me if it is legal or not.
>
> Let's assume it is legal. Then, how will you initialize it?
Therein lies the crux of the problem. If we take the position that
'void&' means 'untyped reference', then we are left in the position
of either
A) disallowing it entirely (because it can't be initialized or
passed as a function argument), which is the situation as it stands
today, or
B) allowing it to be initialized to refer to an object of any type
(much akin to assignments to 'void*'), effectively introducing the
concept of an "any" type to the language.
Currently, 'void' has two distinct meanings:
1) A 'void()' function returns no value;
2) A 'void*' is a pointer to an object of indeterminant type, i.e.,
it is an "untyped pointer";
Added 'void&' would add a third meaning:
3) A 'void&' is a reference to an object of indeterminant type,
i.e., it is an "untyped reference".
Of course, given (B), it's still problematic to actually use the
object referred to by a 'void&' reference, since you would have to
cast it to a well-defined type before you could access its value or
members.
At this point in the thread, I don't see any practical use for
'void&'. But then I only asked the question from a theoretical
standpoint to begin with.
-- David R. Tribble, dtribble@technologist.com --
---
[ 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: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/05/26 Raw View
In article <3749B5EC.A0077901@technologist.com>,
David R Tribble <dtribble@technologist.com> wrote:
>Added 'void&' would add a third meaning:
> 3) A 'void&' is a reference to an object of indeterminant type,
> i.e., it is an "untyped reference".
>
>Of course, given (B), it's still problematic to actually use the
>object referred to by a 'void&' reference, since you would have to
>cast it to a well-defined type before you could access its value or
>members.
That's analogous to the way void* is used.
>At this point in the thread, I don't see any practical use for
>'void&'. But then I only asked the question from a theoretical
>standpoint to begin with.
It seems to me that it could be used similarly to void*.
typedef enum {thing_is_int, thing_is_float} thing_type;
void set_thing(void& thing, thing_type type, void* thing_value) {
int &int_thing = thing;
float &float_thing = thing;
switch (type) {
thing_is_int:
int_thing = *(int *)thing_value;
break;
thing_is_float:
float_thing = *(float *)thing_value;
}
return;
}
This particular example could be written better using templates, but
perhaps there are examples that couldn't. For instance, templates tend to
result in duplicate code for each instantiation, so if this function had
lots of code that wasn't dependent on the type of thing, it would be more
efficient in code space to use a single function with a void& parameter
rather than a templated function.
--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
---
[ 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: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/05/21 Raw View
David R Tribble <dtribble@technologist.com> wrote:
: Is type 'void&' (reference to void) illegal? I can't find it
: explicitly disallowed in 8.3.2 or 8.5.3, so perhaps someone can tell
: me if it is legal or not.
My IS is elsewhere but from CD2 8.3.2/1 Last sentence:
A declarator that specifies the type "reference to cv void"
is ill-formed.
John
[ 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: clamage@eng.sun.com (Steve Clamage)
Date: 1999/05/21 Raw View
fvali@biotrack.com writes:
>Why did the standard's comittee illegalize the void& - there must
>be some obvious reason that justifies making void&'s
>an unacceptable entity in our beloved microcosm we call C++ ;-)
In C++, a reference is not just another syntax for pointers.
References are more restrictive, and unlike pointers, are not
themselves objects. A reference is another way to refer to an
existing object.
Other language design choices are possible, and indeed it
would have been possible to make references in fact just
another syntax for using pointers. It seemed more useful
to provide more guarantees about references, however.
Type void is an incomplete type which cannot be completed.
There are no objects of type void, so it doesn't strictly
make sense to have a reference to void.
You could make basically these same arguments about void*,
but it is useful to have a "generic pointer". Type void*
is therefore a special case meaning "pointer to an object
of unknown type", which (like any pointer) could also be null.
You can pass around by value or otherwise assign void pointers
with no conceptual difficulty. Not so with references.
You could use a void& to initialize another void&, but that's
about all. You couldn't use a void& in any other expression
context, because use of a reference means the object to which it
refers -- and that there isn't one for void&. To use a void&,
you almost always need to take the address of the void&,
negating most of the advantages.
I don't see that void& would provide any additional expressive
power or benefits, given the properties of void in C++.
--
Steve Clamage, stephen.clamage@sun.com
[ 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/22 Raw View
David R Tribble wrote:
>
> Is type 'void&' (reference to void) illegal? I can't find it
> explicitly disallowed in 8.3.2 or 8.5.3, so perhaps someone can tell
> me if it is legal or not.
>
Let's assume it is legal. Then, how will you initialize it?
--
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 ]
Author: deepblack@geocities.com (Luis Coelho)
Date: 1999/05/20 Raw View
On 18 May 99 17:47:13 GMT, jpotter@falcon.lhup.edu (John Potter)
uttered the following words:
>fvali@biotrack.com wrote:
>Simply stated, references are aliases for things and void(nothing) is
>not a thing. A void* is a pointer to anything which includes nothing;
>however, a void& is nonsence.
void* is also nonsense. I mean a 'pointer to nothing'?
But seriously, IMHO void* reuses the keyword void, when in fact
"type void" (one can also question whether void is a type; it
could get philosofical very fast, though) is very different from
what is pointed to by a void*, which is an undefined area of
memory.
Regards,
Luis Coelho.
C++ Programming Language, 3rd Ed. by B. Stroustrup. My exercise answers at:
http://www.geocities.com/SiliconValley/Way/3972/index.html
---
[ 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: deepblack@geocities.com (Luis Coelho)
Date: 1999/05/17 Raw View
On 14 May 99 15:50:43 GMT, David R Tribble
<dtribble@technologist.com> uttered the following words:
>I'm not sure of what use it would be, and I have a gut feeling that
>it ought to be illegal, but conceivably it could be used as something
>akin to type 'void*'.
>
>Consider:
>
> void foo(const void &o) // a la 'const void *'
> void bar()
> {
> SomeType obj;
>
> foo(obj); // Does not need an '&'
> ...
> }
This could be achieved with
<code>
void real_foo(const void*);
template <typename T>
void foo(const T& t)
{
real_foo(&t);
}
</code>
BTW, g++ rejects your code parsing your foo as void foo(void);
Regards
Luis Coelho.
C++ Programming Language, 3rd Ed. by B. Stroustrup. My exercise answers at:
http://www.geocities.com/SiliconValley/Way/3972/index.html
[ 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: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/05/18 Raw View
fvali@biotrack.com wrote:
: Why did the standard's comittee illegalize the void& - there must
: be some obvious reason that justifies making void&'s
: an unacceptable entity in our beloved microcosm we call C++ ;-)
Simply stated, references are aliases for things and void(nothing) is
not a thing. A void* is a pointer to anything which includes nothing;
however, a void& is nonsence.
The comittee may have other answers.
John
---
[ 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: David R Tribble <dtribble@technologist.com>
Date: 1999/05/14 Raw View
Is type 'void&' (reference to void) illegal? I can't find it
explicitly disallowed in 8.3.2 or 8.5.3, so perhaps someone can tell
me if it is legal or not.
I'm not sure of what use it would be, and I have a gut feeling that
it ought to be illegal, but conceivably it could be used as something
akin to type 'void*'.
Consider:
void foo(const void &o) // a la 'const void *'
{
const SomeType * p;
p = static_cast<const SomeType *>(&o);
... use p to access 'o' as a 'SomeType' object
}
void bar()
{
SomeType obj;
foo(obj); // Does not need an '&'
...
}
-- David R. Tribble, dtribble@technologist.com --
---
[ 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: fvali@biotrack.com
Date: 1999/05/14 Raw View
8.3.2 paragraph 1's last sentence reads:
"A declarator that specifies the type \"reference to cv void\" is
ill-formed."
Initially I was convinced that had it been allowed it would not
have served any useful purpose whatsoever - but i'm not so sure
it's that simple.
Let me attempt to delineate some of the 'legitimate' uses of void*'s,
that I can think of:
a) As parameter types in callbacks (although a union of various types,
might be a better option - if type-unsafe callbacks must be used)
b) As return types from allocators (though SL's allocators don't)
c) As return types from class factories (although with the
use of proxy objects, this might also be unnecessary) (similar to b)
d) In partial specilizations to reduce code bloat (e.g. generic
containers)
e) To store an array of addresses pointing to a quantized chunk of
memory (memory managers)
f) hmm what else...
Except for (e), i believe in the other cases void references could be
swapped for void*s (though there might be some special cases when
using them in templates).
Why did the standard's comittee illegalize the void& - there must
be some obvious reason that justifies making void&'s
an unacceptable entity in our beloved microcosm we call C++ ;-)
-fais
In article <373B7233.21386E4A@technologist.com>,
David R Tribble <dtribble@technologist.com> wrote:
> Is type 'void&' (reference to void) illegal? I can't find it
> explicitly disallowed in 8.3.2 or 8.5.3, so perhaps someone can tell
> me if it is legal or not.
>
> I'm not sure of what use it would be, and I have a gut feeling that
> it ought to be illegal, but conceivably it could be used as something
> akin to type 'void*'.
>
> Consider:
>
> void foo(const void &o) // a la 'const void *'
> {
> const SomeType * p;
>
> p = static_cast<const SomeType *>(&o);
> ... use p to access 'o' as a 'SomeType' object
> }
>
> void bar()
> {
> SomeType obj;
>
> foo(obj); // Does not need an '&'
> ...
> }
>
<snip>
--
Faisal Vali fvali@biotrack.com
Software Engineer Identix Biometric Security
WWW: http://www.identix.com
-------------------------------------------------------
It's a miserable ritual, a magical procedure. . . a homunculus of the
consciousness of the new world -- our world passed away and a new world
has arisen. -- Carl Jung on Ulysses, in the Europaeische Revue
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
[ 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 ]