Topic: Why no anonymous POD struct declaration in sizeof?


Author: algrant@myrealbox.com (Al Grant)
Date: Tue, 10 Sep 2002 08:40:10 +0000 (UTC)
Raw View
kuyper@wizard.net ("James Russell Kuyper Jr.") wrote in message news:<3D7699EA.8F21618A@wizard.net>...
> Al Grant wrote:
> > > >  ... sizeof(struct { char c; int n; }) ...
> > >
> > I wouldn't have asked if I hadn't had to deal with it in real code.
>
> You "had" to deal with it? Why? What prevented you from using normal
> good practice: giving the struct a type name, such as char_int, and
> using sizeof(char_int)?

I did that.  This was existing C code which needed to compile as C++.
Of course, I scoped the type in a minimal enclosing block, so this
feature failed to enforce global type sharing.

> Good coding practice requires that any user-defined type that you're
> going to use in two or more different places should have only a single
> definition that is in scope in every place it's used.

But such pragmatic arguments make no sense of the fact that you
can use very complicated type expressions in expressions - such as
function pointer types in casts - and, above all, you can even say

  sizeof(std::pair<char,int>)

The fact that even a trivial POD struct specifier "defines" a type
in a way that the others don't, is a feature of C++.  Those with
exposure to purer languages (e.g. those with a concept of product
types) - as well as C - might consider that POD struct specifiers
should be usable in the same contexts as their names, by the
principles of orthogonality and referential transparency.

> The only possible use of sizeof(Type) is if you plan to use Type
> somewhere else in the code. Therefore, it's used in two or more
> different places, and the above rule applies.

But this applies to sizeof(int).  After all, the only possible
reason (we're told) for asking is if you have an int object.
But the object's type might be changed.  So really you should
only be allowed to apply sizeof to named types.  Right?

And in any case, how is it that this incompatibility with C is
justified on safety grounds, yet regular requests for virtual
override enforcement are rejected, it being said that it's up to
the programmer to check, by inspection and runtime coverage
testing, that overrides have exactly the same type signatures?

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: algrant@myrealbox.com (Al Grant)
Date: Tue, 3 Sep 2002 17:48:53 +0000 (UTC)
Raw View
This is legal C, why is it forbidden in C++?

  ... sizeof(struct { char c; int n; }) ...

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Tue, 3 Sep 2002 18:17:42 +0000 (UTC)
Raw View
In article <5765b025.0209022323.e35c941@posting.google.com>, Al Grant
<algrant@myrealbox.com> writes
>This is legal C, why is it forbidden in C++?
>
>  ... sizeof(struct { char c; int n; }) ...

Perhaps because there is no conceivable reason to use it. :-)

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: algrant@myrealbox.com (Al Grant)
Date: Wed, 4 Sep 2002 18:00:46 +0000 (UTC)
Raw View
francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message news:<uKceWREniPd9EwRp@robinton.demon.co.uk>...
> In article <5765b025.0209022323.e35c941@posting.google.com>, Al Grant
> <algrant@myrealbox.com> writes
> >This is legal C, why is it forbidden in C++?
> >
> >  ... sizeof(struct { char c; int n; }) ...
>
> Perhaps because there is no conceivable reason to use it. :-)

Why not?  It's layout-compatible with POD struct types you can name
and apply sizeof() to.  Are you saying that you might not get the
same result?

I wouldn't have asked if I hadn't had to deal with it in real code.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Ken@Alverson.net ("Ken Alverson")
Date: Wed, 4 Sep 2002 20:55:57 +0000 (UTC)
Raw View
"Al Grant" <algrant@myrealbox.com> wrote in message
news:5765b025.0209040924.6a536772@posting.google.com...
> francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message
news:<uKceWREniPd9EwRp@robinton.demon.co.uk>...
> > In article <5765b025.0209022323.e35c941@posting.google.com>, Al
Grant
> > <algrant@myrealbox.com> writes
> > >This is legal C, why is it forbidden in C++?
> > >
> > >  ... sizeof(struct { char c; int n; }) ...
> >
> > Perhaps because there is no conceivable reason to use it. :-)
>
> Why not?  It's layout-compatible with POD struct types you can name
> and apply sizeof() to.  Are you saying that you might not get the
> same result?

Correctness or not aside, the construct is a maintnance nightmare.  The
only logical reason it would be used is to calculate the size of a known
object type.  And if the object type is known, why is it being
respecified instead of using the type as the compiler already knows it?
This is just asking to be overlooked when the object type changes,
causing subtle and difficult to track problems.

Ken


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Thu, 5 Sep 2002 14:16:04 +0000 (UTC)
Raw View
In article <al5osg$ne7$1@eeyore.INS.cwru.edu>, Ken Alverson
<Ken@Alverson.net> writes
>"Al Grant" <algrant@myrealbox.com> wrote in message
>news:5765b025.0209040924.6a536772@posting.google.com...
>> francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message
>news:<uKceWREniPd9EwRp@robinton.demon.co.uk>...
>> > In article <5765b025.0209022323.e35c941@posting.google.com>, Al
>Grant
>> > <algrant@myrealbox.com> writes
>> > >This is legal C, why is it forbidden in C++?
>> > >
>> > >  ... sizeof(struct { char c; int n; }) ...
>> >
>> > Perhaps because there is no conceivable reason to use it. :-)
>>
>> Why not?  It's layout-compatible with POD struct types you can name
>> and apply sizeof() to.  Are you saying that you might not get the
>> same result?
>
>Correctness or not aside, the construct is a maintnance nightmare.  The
>only logical reason it would be used is to calculate the size of a known
>object type.  And if the object type is known, why is it being
>respecified instead of using the type as the compiler already knows it?
>This is just asking to be overlooked when the object type changes,
>causing subtle and difficult to track problems.

And the technique was/is required for C code. However, IIRC, it causes
many headaches because the more usual version:

sizeof(struct X {char c; int n; })

either referred to an already in scope struct X, or if none such existed
it defined such a type which immediately vanished. That alone would be
good reason to require a diagnostic in C++ (implementations are free to
support the syntax as long as they issue at least one diagnostic in
every TU using it)


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net ("James Russell Kuyper Jr.")
Date: Thu, 5 Sep 2002 18:02:50 +0000 (UTC)
Raw View
Al Grant wrote:
>
> francis.glassborow@ntlworld.com (Francis Glassborow) wrote in message news:<uKceWREniPd9EwRp@robinton.demon.co.uk>...
> > In article <5765b025.0209022323.e35c941@posting.google.com>, Al Grant
> > <algrant@myrealbox.com> writes
> > >This is legal C, why is it forbidden in C++?
> > >
> > >  ... sizeof(struct { char c; int n; }) ...
> >
> > Perhaps because there is no conceivable reason to use it. :-)
>
> Why not?  It's layout-compatible with POD struct types you can name
> and apply sizeof() to.  Are you saying that you might not get the
> same result?
>
> I wouldn't have asked if I hadn't had to deal with it in real code.

You "had" to deal with it? Why? What prevented you from using normal
good practice: giving the struct a type name, such as char_int, and
using sizeof(char_int)?

Good coding practice requires that any user-defined type that you're
going to use in two or more different places should have only a single
definition that is in scope in every place it's used. That definition
might appear in multiple different translation units, but through the
magic of #include's, it can and should reside in one, and only one,
physical source code file. Any other approach leads to having different
copies of the definition updated inconsistently with each other.

The only possible use of sizeof(Type) is if you plan to use Type
somewhere else in the code. Therefore, it's used in two or more
different places, and the above rule applies.

Since the proposed feature is currently illegal, has a trivial
work-around, and since failing to use the work-around is bad coding
practice, I don't see much point in making the proposed feature legal.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]