 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ThosRTanner Guest
|
Posted: Fri Feb 24, 2006 11:06 am Post subject: type declarations in anonymous unions |
|
|
We are testing a new version of our vendors C++ compiler, and it has
decided to give warnings about this:
class xxxx
{
union
{
uint32_t word;
struct
{
int this_one : 1;
int that_one: 1;
int a_bit_more: 5;
int something_else: 12;
int the_rest: 13;
} bits;
};
//etc etc
}
It now says "Types cannot be declared in an anonymous union".
Is this true? It seems pretty harmless. I can see that you wouldn't be
able to reference the declared types outside the union, but I would
have thought that would cause compliation errors anyway.
Any thoughts?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sun Feb 26, 2006 12:06 pm Post subject: Re: type declarations in anonymous unions |
|
|
ThosRTanner wrote:
| Quote: | We are testing a new version of our vendors C++ compiler, and it has
decided to give warnings about this:
class xxxx
{
union
{
uint32_t word;
struct
{
int this_one : 1;
int that_one: 1;
int a_bit_more: 5;
int something_else: 12;
int the_rest: 13;
} bits;
};
//etc etc
}
It now says "Types cannot be declared in an anonymous union".
Is this true? It seems pretty harmless. I can see that you wouldn't be
able to reference the declared types outside the union, but I would
have thought that would cause compliation errors anyway.
Any thoughts?
|
I think the potential problem would arise for a named type declared
within an anonymous union. Presumably, another anonymous union could
declare a nested type with the same name, and the two names despite
their different scopes would collide. And rather to make matters any
more complicated, the rule bans any "nested type" from being declared
inside a union, so unnamed types are prohibited as well.
The fix is to move the declaration outside of the union. I am assuming
that "nested" means "nested inside the union" and not nested elsewhere.
With that assumption, a class scope declaration should be acceptable:
struct xxx
{
struct bitfield
{
int this_one : 1;
int that_one: 1;
int a_bit_more: 5;
int something_else: 12;
int the_rest: 13;
};
union
{
uint32_t word;
bitfield bits;
};
};
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alex Beluga Guest
|
Posted: Sun Feb 26, 2006 5:06 pm Post subject: Re: type declarations in anonymous unions |
|
|
ThosRTanner wrote:
| Quote: | We are testing a new version of our vendors C++ compiler, and it has
decided to give warnings about this:
... lots of code
It now says "Types cannot be declared in an anonymous union".
Is this true? It seems pretty harmless. I can see that you wouldn't be
able to reference the declared types outside the union, but I would
have thought that would cause compliation errors anyway.
|
Greetings!
In my opinion, answer to your problem can be found in the C++ standart
documentation.
2005th draft of it says in 9.5.2 that
"[ Note: nested types and functions cannot be declared within an
anonymous union. --end note]
Consequently, if you aim to be a C++-standart-compliant compiler, it
will make sense to throw errors on this.
As an additional argument to my statement I'd like to say that Comeau
Online didn't compile this code, throwing errors at struct declaration.
Sincerely yours, Aleksander Beluga.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
ThosRTanner Guest
|
Posted: Mon Feb 27, 2006 11:06 am Post subject: Re: type declarations in anonymous unions |
|
|
Greg Herlihy wrote:
| Quote: | ThosRTanner wrote:
We are testing a new version of our vendors C++ compiler, and it has
decided to give warnings about this:
class xxxx
{
union
{
uint32_t word;
struct
{
int this_one : 1;
int that_one: 1;
int a_bit_more: 5;
int something_else: 12;
int the_rest: 13;
} bits;
};
//etc etc
}
It now says "Types cannot be declared in an anonymous union".
Is this true? It seems pretty harmless. I can see that you wouldn't be
able to reference the declared types outside the union, but I would
have thought that would cause compliation errors anyway.
Any thoughts?
I think the potential problem would arise for a named type declared
within an anonymous union. Presumably, another anonymous union could
declare a nested type with the same name, and the two names despite
their different scopes would collide.
If you can't access the name outside the union, I can't see how the |
names could collide.
| Quote: | And rather to make matters any
more complicated, the rule bans any "nested type" from being declared
inside a union, so unnamed types are prohibited as well.
It makes them more complicated for me! |
| Quote: | The fix is to move the declaration outside of the union. I am assuming
that "nested" means "nested inside the union" and not nested elsewhere.
With that assumption, a class scope declaration should be acceptable:
struct xxx
{
struct bitfield
{
int this_one : 1;
int that_one: 1;
int a_bit_more: 5;
int something_else: 12;
int the_rest: 13;
};
union
{
uint32_t word;
bitfield bits;
};
};
That would work. It just sort of offends my feeling that you should not |
extend the scope of any declaration beyond where it is needed, because
if you do - it'll get misused, and when it does, it is usually absolute
hell to fix it.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Thu Mar 02, 2006 12:06 am Post subject: Re: type declarations in anonymous unions |
|
|
ThosRTanner wrote:
| Quote: | Greg Herlihy wrote:
ThosRTanner wrote:
We are testing a new version of our vendors C++ compiler, and it has
decided to give warnings about this:
class xxxx
{
union
{
uint32_t word;
struct
{
int this_one : 1;
int that_one: 1;
int a_bit_more: 5;
int something_else: 12;
int the_rest: 13;
} bits;
};
//etc etc
}
It now says "Types cannot be declared in an anonymous union".
Is this true? It seems pretty harmless. I can see that you wouldn't be
able to reference the declared types outside the union, but I would
have thought that would cause compliation errors anyway.
Any thoughts?
I think the potential problem would arise for a named type declared
within an anonymous union. Presumably, another anonymous union could
declare a nested type with the same name, and the two names despite
their different scopes would collide.
If you can't access the name outside the union, I can't see how the
names could collide.
|
But you can - so they would. In fact, about the only reason for
declaring an anonymous is exactly for its economy of syntax. For
example:
struct xxx
{
struct bitfield
{
int this_one : 1;
int that_one: 1;
int a_bit_more: 5;
int something_else: 12;
int the_rest: 13;
};
union
{
uint32_t word;
bitfield bits;
};
};
int main()
{
xxx x;
x.bits.this_one = 1;
}
Using an anonymous union in this case eliminated one of the "."
operands that would otherwise be needed to specify the bitfield
"this_one" within a named union in x.
| Quote: | That would work. It just sort of offends my feeling that you should not
extend the scope of any declaration beyond where it is needed, because
if you do - it'll get misused, and when it does, it is usually absolute
hell to fix it.
|
The purpose of the anonymous union, as I see it, is to make the fields
inside the union more (and not less) accessible to the outside. So much
so, that without this restriction against a nested declaration, the
syntax used to access an anonymous union could be at times ambiguous.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|