 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Le Chaud Lapin Guest
|
Posted: Sat Nov 26, 2005 3:06 pm Post subject: Just When I Was Getting Used To Using Using - Arrgg |
|
|
I have a class Packet that contains a nested class Payload and an
enumeration:
struct Packet
{
struct Payload
{
} ;
} ;
I had gotten into the habit of writing:
using Packet::Payload;
so I could write:
Payload::..blah..
instead of
Packet::Payload::..blah
I have done this... (gulp) in many, many .cpp files.
I tried out the new Visual C++ Express compiler from you-know-who on my
source code and got 1000's of error messages:
...PacketPacket.cpp(13) : error C2885: 'Packet::Payload': not a valid
using-declaration at non-class scope
What does the standard say? Is this really illegal as Microsoft says?
And if it is illegal, what is the rationale behind making it so?
Reference: http://msdn2.microsoft.com/en-us/library/ms177253.aspx
-Le Chaud Lapin-
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Sat Nov 26, 2005 4:37 pm Post subject: Re: Just When I Was Getting Used To Using Using - Arrgg |
|
|
On 26 Nov 2005 10:06:23 -0500, "Le Chaud Lapin"
<unoriginal_username (AT) yahoo (DOT) com> wrote:
| Quote: | I have a class Packet that contains a nested class Payload and an
enumeration:
struct Packet
{
struct Payload
{
} ;
} ;
I had gotten into the habit of writing:
using Packet::Payload;
so I could write:
Payload::..blah..
instead of
Packet::Payload::..blah
I have done this... (gulp) in many, many .cpp files.
I tried out the new Visual C++ Express compiler from you-know-who on my
source code and got 1000's of error messages:
..PacketPacket.cpp(13) : error C2885: 'Packet::Payload': not a valid
using-declaration at non-class scope
What does the standard say? Is this really illegal as Microsoft says?
|
Yes.
See section 7.3.3 paragraph 6. Since Packet is a typename, "using
Packet::Payload;" is a member using declaration. However, you are not
inside a class scope which is derived from Packet, so it is illegal.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ 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 Nov 27, 2005 4:02 am Post subject: Re: Just When I Was Getting Used To Using Using - Arrgg |
|
|
On 11/26/05 7:06 AM, in article
[email]1132983450.111450.94000 (AT) g47g2000cwa (DOT) googlegroups.com[/email], "Le Chaud Lapin"
<unoriginal_username (AT) yahoo (DOT) com> wrote:
| Quote: | I have a class Packet that contains a nested class Payload and an
enumeration:
struct Packet
{
struct Payload
{
} ;
} ;
I had gotten into the habit of writing:
using Packet::Payload;
so I could write:
Payload::..blah..
instead of
Packet::Payload::..blah
I tried out the new Visual C++ Express compiler from you-know-who on my
source code and got 1000's of error messages:
..PacketPacket.cpp(13) : error C2885: 'Packet::Payload': not a valid
using-declaration at non-class scope
What does the standard say? Is this really illegal as Microsoft says?
And if it is illegal, what is the rationale behind making it so?
|
A using declaration for a class member is a member declaration and as such
must appear within a class definition, just like any other member. So the
error message is correct: it is not legal to have a "using Packet::Payload"
declaration outside of a class scope, since "Packet" is a class and not a
namespace.
The only "rationale" that I can discern is simply the fact that C++ does not
allow class members to be declared outside of their class definition. And
there would seem little to gain in this case from making an exception.
After all, it is possible to bring Payload into the global namespace without
a using declaration. So adding a redundant technique for globalizing names
would serve only to increase the chance of accidentally publishing class
names in the global scope, and not provide the user the benefit with any new
capability to compensate for the added risk.
So instead of a making a using declaration, a program that wants to have
"Payload" refer to "Packet::Payload" (without moving the Payload class
outside of Packet) should use this declaration instead:
typedef Packet::Payload Payload;
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Sun Nov 27, 2005 4:05 am Post subject: Re: Just When I Was Getting Used To Using Using - Arrgg |
|
|
Bob Hairgrove wrote:
| Quote: |
See section 7.3.3 paragraph 6. Since Packet is a typename, "using
Packet::Payload;" is a member using declaration. However, you are not
inside a class scope which is derived from Packet, so it is illegal.
|
Correct. The correct way of introducing a member name in a different
scope is with a "normal" typedef:
typedef Packet::Payload Payload;
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Le Chaud Lapin Guest
|
Posted: Sun Nov 27, 2005 5:53 pm Post subject: Re: Just When I Was Getting Used To Using Using - Arrgg |
|
|
Greg Herlihy wrote:
| Quote: | The only "rationale" that I can discern is simply the fact that C++ does not
allow class members to be declared outside of their class definition. And
there would seem little to gain in this case from making an exception.
|
Yes, I still had some fuzzies, but this makes sense. In other words,
why pollute the namespace for no good reason? Extracting names out of
classes into the global namespace just to avoid a bit of typing is
asking for trouble, real namespaces notwithstanding.
| Quote: | So instead of a making a using declaration, a program that wants to have
"Payload" refer to "Packet::Payload" (without moving the Payload class
outside of Packet) should use this declaration instead:
typedef Packet::Payload Payload;
|
And given that one would add a new type just to have an aliases, I
decided to go ahead and use full qualification (Packet::Payload)
throughout:
Thanks for your clear explanation.
-Le Chaud Lapin-
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Mon Nov 28, 2005 3:50 pm Post subject: Re: Just When I Was Getting Used To Using Using - Arrgg |
|
|
Le Chaud Lapin wrote:
| Quote: | Greg Herlihy wrote:
So instead of a making a using declaration, a program that wants to have
"Payload" refer to "Packet::Payload" (without moving the Payload class
outside of Packet) should use this declaration instead:
typedef Packet::Payload Payload;
And given that one would add a new type just to have an aliases, I
decided to go ahead and use full qualification (Packet::Payload)
throughout:
|
Hmmm, I was under the impression that a typedef did not introduce a new type
but only an alias for an existing one. That's also a reason you can't
overload on a typedef.
Anyhow, reading the above I wonder what is the difference between these two
lines here:
using std::string;
typedef std::string string;
I'd expect them to be roughly equivalent, but are there corner cases where
that is not the case?
Uli
[ 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
|
|