Topic: empty objects, void type (was: size of empty class objects)
Author: "Paul D. DeRocco" <strip_these_words_pderocco@ix.netcom.com>
Date: 1997/05/11 Raw View
Videoman wrote:
> Speaking of which, what happened with the the suggestion that void be
> given full and equal type status, mainly to prevent templates from
> breaking if instanciated with a type of void and attempting to return
> it? I seem to remember Bjarne S. himself saying he was going to
> suggest it to the commiteee on a post here roughly a year or so ago.
I'm one of the (probably) many people who also sent a proposal to that
effect to the standard committee.
> I personally support the idea whole-heartedly, as it would also allow
> quite a few other things, such as the use of functional notation in
> source code to call nested functions returning void.
>
> For example:
>
> void foo1(void);
> void foo2(void);
>
> void foo3(void)
> {
> return foo1(foo2());
> }
Yep. Really useful in templates.
> The only problem would be the div-by-zero problem attempting to
> calculate the size of an array of void type.
I think sizeof(void) should simply be undefined, just as sizeof any
function type is undefined.
> Personally, I think it would be possible for a compiler to determine
> if the array was of size zero, and fudge it to be size 1 if it were
> used in a division as above.
Blecchh.
> Perhaps it is time to define an array_sizeof() operator, since it
> seems to be needed anyways, and use of that operator would eliminate
> nearly all the problems caused by zero-sized types.
I'd call it lengthof. It would be equal to sizeof(a)/sizeof(*a) for
existing arrays, but it would be built into the compiler, which would be
smart enough to complain (or return 1) if it were applied to a non-array
type. If you start with lengthof, then all combinations of array
arithmetic would work even with zero-sized elements (other than the lack
of uniqueness).
> The address of an object of type void would be NULL, and sizeof(void)
> == 0 by definition. (Although the opposite would not necessarily be
> true.)
Why would the address of an object of type void be NULL? Even if its
size is zero doesn't mean it doesn't have an address. (And the type of
the address would of course be void*).
I also proposed that void be allowed as the type for an externally
defined name; the only thing you'd be able to do with the name (without
casting) is take its address.
--
Ciao,
Paul
(Please remove the "strip_these_words_" prefix from the return
address, which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: videoman@tiac.net (Videoman)
Date: 1997/05/09 Raw View
On 06 May 1997 12:56:38 PDT, meem <meem@shep12.wustl.edu> wrote:
>i disagree. i don't think it's a common case at all. for example,
>C does not specify what the size of an empty `struct' is, and gcc
>makes sizeof() an empty struct == 0.
>
>as jeff said, if the programmer is intentionally creating an empty
>struct (or class) it is likely they know what they're doing, and will
>be smart enough to allocate a char or something to use in case of any
>pointer comparisons. i think this is just another situation where c++
>tries to protect the novice programmer from shooting themselves in the
>foot, but instead ends up befuddling the intermediate and advanced.
Why not suggest to compilier vendors an additional switch to force
classes with no data members or virtual functions to occupy zero
storage for that translation unit?
>incidentally, another argument against zero-sized structs or classes
>is the classic
>
> const size_t n_elems = sizeof arr / sizeof arr[0]; // or some variant
Speaking of which, what happened with the the suggestion that void be
given full and equal type status, mainly to prevent templates from
breaking if instanciated with a type of void and attempting to return
it? I seem to remember Bjarne S. himself saying he was going to
suggest it to the commiteee on a post here roughly a year or so ago.
I personally support the idea whole-heartedly, as it would also allow
quite a few other things, such as the use of functional notation in
source code to call nested functions returning void.
For example:
void foo1(void);
void foo2(void);
void foo3(void)
{
return foo1(foo2());
}
The only problem would be the div-by-zero problem attempting to
calculate the size of an array of void type.
Personally, I think it would be possible for a compiler to determine
if the array was of size zero, and fudge it to be size 1 if it were
used in a division as above.
Perhaps it is time to define an array_sizeof() operator, since it
seems to be needed anyways, and use of that operator would eliminate
nearly all the problems caused by zero-sized types.
The address of an object of type void would be NULL, and sizeof(void)
== 0 by definition. (Although the opposite would not necessarily be
true.)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]