Topic: is struct {int x;}; legal?
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/02/14 Raw View
On Sat, 12 Feb 2000 07:15:09 CST, David R Tribble <david@tribble.com>
wrote:
>Ron Natalie wrote:
>>
>> Horst Kraemer wrote:
>>>
>>> aitken@halcyon.com (William E. Aitken) wrote:
>>>
>>>> G++ diagnoses the following construct as an error.
>>>
>>> struct {int x;};
>>>
>...
>>> This definition is completely useless because you never may define
>>> objects of this unnamed type nor may you refer to it - but
>>> nevertheless it is legal.
>>
>> Nope, it is both useless and illegal.
>
>But anonymous structs have their uses in the contexts of typedefs:
>
> typedef
> struct { int x; }
> Type;
>
>(This might not be exactly on-topic, as the discussion seems to be
>concerned only with Aitken's original construct.)
That is not an anonymous struct. It is a struct that lacks a tag. An
anonymous struct is a struct definition that does not declare a tag,
object or type name. It is not legal in standard C or C++, although
some compilers allow it as an extension. (I don't know why.)
Structs without tags are not a good idea in C++. Your example has no
benefits in C++ compared to
struct Type { int x; };
It creates complications when the struct is more elaborate,
particularly when there might be other typedefs referring to the
struct. (What is the name of the constructor or destructor?)
If the code must be shared with C code, and the C code is trying to
avoid writing "struct Type" all the time instead of just "Type", you
can give the struct a tag with the same name as the typedef:
typedef struct Type { ... } Type;
---
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: James Kuyper <kuyper@wizard.net>
Date: 2000/02/15 Raw View
Steve Clamage wrote:
>
> On Sat, 12 Feb 2000 07:15:09 CST, David R Tribble <david@tribble.com>
> wrote:
....
> >But anonymous structs have their uses in the contexts of typedefs:
> >
> > typedef
> > struct { int x; }
> > Type;
> >
> >(This might not be exactly on-topic, as the discussion seems to be
> >concerned only with Aitken's original construct.)
>
> That is not an anonymous struct. It is a struct that lacks a tag. An
> anonymous struct is a struct definition that does not declare a tag,
> object or type name. It is not legal in standard C or C++, although
> some compilers allow it as an extension. (I don't know why.)
I assume anonymous structs make their members visible in the enclosing
scope, just like anonymous unions? In that case the only point I can see
in allowing them would be that doing so would allow the creation of
block-scope bit-fields. If that's the purpose, I think it's better
served by allowing them to be declared directly, than through anonymous
structs.
---
[ 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: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@germany.sun.com>
Date: 2000/02/15 Raw View
Am 14.02.00, 12:57:50, schrieb Steve Clamage <stephen.clamage@sun.com>
zum Thema Re: is struct {int x;}; legal?:
> David R Tribble <david@tribble.com> wrote:
> >Ron Natalie wrote:
> >>
> >> Horst Kraemer wrote:
> >>>
> >>> struct {int x;};
> >>>
> >>> This definition is completely useless because you never may define
> >>> objects of this unnamed type nor may you refer to it - but
> >>> nevertheless it is legal.
> >>
> >> Nope, it is both useless and illegal.
> >
> >But anonymous structs have their uses in the contexts of typedefs:
> >
> > typedef
> > struct { int x; }
> > Type;
> >
> >(This might not be exactly on-topic, as the discussion seems to be
> >concerned only with Aitken's original construct.)
> That is not an anonymous struct. It is a struct that lacks a tag. An
> anonymous struct is a struct definition that does not declare a tag,
> object or type name. It is not legal in standard C or C++, although
> some compilers allow it as an extension. (I don't know why.)
The only use I can see would be analogous to anonymous unions - and
might be convenient within the context of a union:
union X
{
struct // here
{
unsigned char data1, data2;
unsigned short data3;
}; // export member names to enclosing union
unsigned long packed_data;
};
int main()
{
X uX = {{ 'A','O', 54321 }};
std::cout << std::hex
<< "Packed value is: " << uX.packed_data << '\n';
std::cout << "First Data bytes are: "
<< int(uX.data1) << ", " << int(uX.data2)
<< std::endl;
}
IMHO that is confusing though (as are anonymous unions). Just adding a
struct tag would remove the data members, as the declaration then just
declares a nested type.
--
J rg Barfurth
---
[ 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: Hyman Rosen <hymie@prolifics.com>
Date: 2000/02/04 Raw View
aitken@halcyon.com (William E. Aitken) writes:
> G++ diagnoses the following construct as an error.
> struct {int x;};
> can somone please point me at the language in the standard that
> prohibits this construct?
7/3.
---
[ 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: Thomas Regner <tomte@subdimension.com>
Date: 2000/02/04 Raw View
William E. Aitken wrote:
:: G++ diagnoses the following construct as an error.
::
:: struct {int x;};
::
:: can somone please point me at the language in the standard that
:: prohibits this construct?
::
:: Thanks
I don t think anonymous structs are allowed anywhere in the standard,
choose a name for your baby ;)
struct foo {
int x;
};
should do the job
regards
Tom
--
-^^^^-------------------^^^^--
| Thomas Regner |
| Kaiserstr. 5 |
| 26122 Oldenburg |
| Tel.: +49 441 7775005 |
| E.: tomte@subdimension.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: Ron Natalie <ron@sensor.com>
Date: 2000/02/04 Raw View
Horst Kraemer wrote:
>
> On Thu, 3 Feb 2000 20:44:34 CST, aitken@halcyon.com (William E.
> Aitken) wrote:
>
> > G++ diagnoses the following construct as an error.
>
> Which G++ ? in which context ? My GNU g++ doesn't.
You have to use the "disable stupid-assed gnu extensions" switch called
-pedantic-errors.
>
> This definition is completely useless because you never may define
> objects of this unnamed type nor may you refer to it - but
> nevertheless it is legal.
Nope, it is both useless and illegal.
---
[ 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: aitken@halcyon.com (William E. Aitken)
Date: 2000/02/04 Raw View
In article <389987CA.3DBB2681@wizard.net>,
James Kuyper <kuyper@wizard.net> wrote:
>
>"William E. Aitken" wrote:
>>
>> G++ diagnoses the following construct as an error.
>>
>> struct {int x;};
>>
>> can somone please point me at the language in the standard that
>> prohibits this construct?
>
>I'm curious as to why you want to use this construct? You haven't
>defined anything by it. You haven't defined a named type. You haven't
>defined an object of an anonymous type, because you haven't provided the
>object's name. In short, this code, if legal, would be functionally
>equivalent to a whitespace.
>
I agree that the construct is useless, and I would like it to be illegal
but I couldn't find any language making it so.
>However, here's the relevant requirement from the standard:
>Section 7, paragraph 3: "In a _simple-declaration_, the optional
>_init-declarator-list_ can be omitted only when declaring a class
>(clause 9) or enumeration (7.2), that is when the decl-specifier-seq
>contains either a _class-specifier_, and _elaborated-type-specifier_
>with a _class-key_, or an _enum-specifier_. In these cases and whenever
>a _class-specifier_ or _enum-specifier_ is present in the
>_decl-specifier-seq_, the identifiers in theses specifiers are among the
>names being declared by the declaration ( as _class-names_,
>_enum-names_, or _enumerators_, depending upon the syntax). In such
>cases, and except for the declaration of an unnamed bit-field (9.6) the
>_decl-specifier-seq_ shall introduce one or more names into the program,
>or shall redeclare a name introduced by a previous declaration."
>
>Now, in your code, you've omitted the the _init-declarator-list_, so
>therefore you're required by the last sentence to introduce one or more
>names into the program. 'x' doesn't count - it's a member name that
>isn't a member of any namable struct type, nor of any namable object of
>an unnamed struct type.
>
This was my initial reading as well, but I'm not sure whether
it really holds up. Is your last sentence (ruling out x as counting)
intended as a paraphrase of the last sentence of clause 7 paragraph 2?
What bothers me is that it seems that the same language would outlaw
the (definitely legal)
union {
int x;
} ;
--
William E. Aitken | Formal verification is the
email: aitken@halcyon.com | future of computer science ---
Snail: 6124 86th Ave SE Mercer Island WA | Always has been, always will be.
===============================================================================
---
[ 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: Sandra Loosemore <sandra@shore.net>
Date: 2000/02/04 Raw View
aitken@halcyon.com (William E. Aitken) writes:
> G++ diagnoses the following construct as an error.
>
> struct {int x;};
>
> can somone please point me at the language in the standard that
> prohibits this construct?
Try the introduction to chapter 7. The general gist of the text is
that a declaration has to introduce or redeclare at least one name,
and your struct declaration doesn't declare either a named class or
any named objects of that type.
-Sandra
---
[ 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: James Kuyper <kuyper@wizard.net>
Date: 2000/02/04 Raw View
"William E. Aitken" wrote:
>
> In article <389987CA.3DBB2681@wizard.net>,
> James Kuyper <kuyper@wizard.net> wrote:
....
> >However, here's the relevant requirement from the standard:
> >Section 7, paragraph 3: "In a _simple-declaration_, the optional
> >_init-declarator-list_ can be omitted only when declaring a class
> >(clause 9) or enumeration (7.2), that is when the decl-specifier-seq
> >contains either a _class-specifier_, and _elaborated-type-specifier_
> >with a _class-key_, or an _enum-specifier_. In these cases and whenever
> >a _class-specifier_ or _enum-specifier_ is present in the
> >_decl-specifier-seq_, the identifiers in theses specifiers are among the
> >names being declared by the declaration ( as _class-names_,
> >_enum-names_, or _enumerators_, depending upon the syntax). In such
> >cases, and except for the declaration of an unnamed bit-field (9.6) the
> >_decl-specifier-seq_ shall introduce one or more names into the program,
> >or shall redeclare a name introduced by a previous declaration."
> >
> >Now, in your code, you've omitted the the _init-declarator-list_, so
> >therefore you're required by the last sentence to introduce one or more
> >names into the program. 'x' doesn't count - it's a member name that
> >isn't a member of any namable struct type, nor of any namable object of
> >an unnamed struct type.
> >
>
> This was my initial reading as well, but I'm not sure whether
> it really holds up. Is your last sentence (ruling out x as counting)
> intended as a paraphrase of the last sentence of clause 7 paragraph 2?
No - I'd never noticed that paragraph before, and I'm not entire sure it
means what I was saying. What I meant was a practical issue: there's no
way to use 'x' from that declaration anywhere outside the struct
declaration itself, so it can't be considered to introduce the name 'x'
into the enclosing scope. Whether there's explicit wording that actually
says so is something I'm unsure of.
> What bothers me is that it seems that the same language would outlaw
> the (definitely legal)
>
> union {
> int x;
> } ;
No - look at the wording: the relevant thing is that the declaration
must introduce one or more names to the program. An anonymous union
introduces its member names into the enclosing scope, a declaration of
an anonymous struct doesn'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 ]
Author: David R Tribble <david@tribble.com>
Date: 2000/02/12 Raw View
Ron Natalie wrote:
>
> Horst Kraemer wrote:
>>
>> aitken@halcyon.com (William E. Aitken) wrote:
>>
>>> G++ diagnoses the following construct as an error.
>>
>> struct {int x;};
>>
...
>> This definition is completely useless because you never may define
>> objects of this unnamed type nor may you refer to it - but
>> nevertheless it is legal.
>
> Nope, it is both useless and illegal.
But anonymous structs have their uses in the contexts of typedefs:
typedef
struct { int x; }
Type;
(This might not be exactly on-topic, as the discussion seems to be
concerned only with Aitken's original construct.)
-- David R. Tribble, david@tribble.com, http://david.tribble.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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/02/13 Raw View
In article <38A44CD9.3232E515@tribble.com>, David R Tribble
<david@tribble.com> writes
>But anonymous structs have their uses in the contexts of typedefs:
>
> typedef
> struct { int x; }
> Type;
Hmm...
What about:
struct X {
int x;
};
// many more layout compatible structs
typedef struct { int x; } *Xtype;
X example;
Xtype xp = (*Xtype) &example;
legal or not?
Francis Glassborow Journal Editor, Association of C & C++ Users
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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: aitken@halcyon.com (William E. Aitken)
Date: 2000/02/03 Raw View
G++ diagnoses the following construct as an error.
struct {int x;};
can somone please point me at the language in the standard that
prohibits this construct?
Thanks
--
William E. Aitken | Formal verification is the
email: aitken@halcyon.com | future of computer science ---
Snail: 6124 86th Ave SE Mercer Island WA | Always has been, always will be.
===============================================================================
---
[ 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: James Kuyper <kuyper@wizard.net>
Date: 2000/02/03 Raw View
"William E. Aitken" wrote:
>
> G++ diagnoses the following construct as an error.
>
> struct {int x;};
>
> can somone please point me at the language in the standard that
> prohibits this construct?
I'm curious as to why you want to use this construct? You haven't
defined anything by it. You haven't defined a named type. You haven't
defined an object of an anonymous type, because you haven't provided the
object's name. In short, this code, if legal, would be functionally
equivalent to a whitespace.
However, here's the relevant requirement from the standard:
Section 7, paragraph 3: "In a _simple-declaration_, the optional
_init-declarator-list_ can be omitted only when declaring a class
(clause 9) or enumeration (7.2), that is when the decl-specifier-seq
contains either a _class-specifier_, and _elaborated-type-specifier_
with a _class-key_, or an _enum-specifier_. In these cases and whenever
a _class-specifier_ or _enum-specifier_ is present in the
_decl-specifier-seq_, the identifiers in theses specifiers are among the
names being declared by the declaration ( as _class-names_,
_enum-names_, or _enumerators_, depending upon the syntax). In such
cases, and except for the declaration of an unnamed bit-field (9.6) the
_decl-specifier-seq_ shall introduce one or more names into the program,
or shall redeclare a name introduced by a previous declaration."
Now, in your code, you've omitted the the _init-declarator-list_, so
therefore you're required by the last sentence to introduce one or more
names into the program. 'x' doesn't count - it's a member name that
isn't a member of any namable struct type, nor of any namable object of
an unnamed struct type.
Now, enumerations are a different matter - its quite possible to declare
a list of enumerators for an unnamed enumeration type, where no objects
have been declared of that type. That's because enumerators are usable
on their own:
enum {LEFT, RIGHT, UP, DOWN};
[ 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: horst.kraemer@t-online.de (Horst Kraemer)
Date: 2000/02/04 Raw View
On Thu, 3 Feb 2000 20:44:34 CST, aitken@halcyon.com (William E.
Aitken) wrote:
> G++ diagnoses the following construct as an error.
Which G++ ? in which context ? My GNU g++ doesn't.
> struct {int x;};
>
> can somone please point me at the language in the standard that
> prohibits this construct?
This definition is completely useless because you never may define
objects of this unnamed type nor may you refer to it - but
nevertheless it is legal.
Regards
Horst
---
[ 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 ]