f



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:

...\Packet\Packet.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! ]

0
11/26/2005 3:06:23 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

5 Replies
1774 Views

Similar Articles

[PageSpeed] 31

On 26 Nov 2005 10:06:23 -0500, "Le Chaud Lapin"
<unoriginal_username@yahoo.com> wrote:

>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:
>
>..\Packet\Packet.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.

>And if it is illegal, what is the rationale behind making it so?
>
>Reference:  http://msdn2.microsoft.com/en-us/library/ms177253.aspx

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
NoSpamPlease@Home.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Bob
11/26/2005 4:37:58 PM
On 11/26/05 7:06 AM, in article
1132983450.111450.94000@g47g2000cwa.googlegroups.com, "Le Chaud Lapin"
<unoriginal_username@yahoo.com> wrote:

> 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:
> 
> ..\Packet\Packet.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! ]

0
Greg
11/27/2005 4:02:45 AM
Bob Hairgrove wrote:
> 
> 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! ]

0
Alberto
11/27/2005 4:05:12 AM
Greg Herlihy wrote:
> 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.

> 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! ]

0
Le
11/27/2005 5:53:01 PM
Le Chaud Lapin wrote:
> 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! ]

0
Ulrich
11/28/2005 3:50:05 PM
Reply:

Similar Artilces:

Using using
I want to plot column 2 against col. 1, but subtract a constant from col. 2. I tried, in a test script, plot 'test.dat' using 1:($2 + 1) w p pt 7 but gnuplot complains Warning: empty y range [1:1], adjusting to [0.99:1.01] although the data file contains 1 1 2 2 3 3 4 4 5 5 What am I doing wrong? I read this in the manual: plot 'file' using 1:($2+$3) "%1f,%1f,%1f" which led me to use the above. I assume the format stuff is not needed. If I am doing this wrong, how do I modify a given column to be plotted arithmetically? -- Dieter...

when to use == and when to use is
I know this question has been answered: http://stackoverflow.com/questions/6570371/when-to-use-and-when-to-use-is , but I still have doubts. Consider the following code: class A: def __init__(self, a): self._a = a #def __eq__(self, other): # return self._a != other._a obj_0 = A(0) obj_1 = A(1) obj_2 = A(2) obj = obj_1 if obj == obj_0: print(0) elif obj == obj_1: print(1) elif obj == obj_2: print(2) if obj is obj_0: print(0) elif obj is obj_1: print(1) elif obj is obj_2: print(2) Both if statements wo...

when to use ; and when to use ,
Any statements like printf("abc"); scanf("%d",&x); a=20; can be replaced by printf("abc"), scanf("%d",&x), a=20; So when should we use ; and when sould we use , In article <1181971596.941107.318810@o11g2000prd.googlegroups.com>, Ravi <ra.ravi.rav@gmail.com> wrote: >Any statements like > printf("abc"); > scanf("%d",&x); > a=20; >can be replaced by > printf("abc"), scanf("%d",&x), a=20; > >So when should we use ; and when sould we use , Use ; when you want ...

when to use C and when to use C++
Hi everyone, I have been asked this question quite a few times and i wonder what could the actual reason? " When to prefer C over C++ or vice versa, of course this is for real world pratical scenario" As per my understanding, C++ makes it easier to program application related logic as OOPS helps in that sense, but for some reason that is not a reasonable explanation, could any one add on their comments? sam_cit@yahoo.co.in wrote: > Hi everyone, > > I have been asked this question quite a few times and i wonder what > could the actual reason? > > &qu...

Using where; Using index vs Using where
Hello, What are the difference between the following statements, which one is better? mysql> desc SELECT COUNT(*) as `cnt` FROM vote WHERE `ip` = INET_ATON('127.0.0.1') AND (`addtime` > NOW() - INTERVAL 1 DAY) ; +----+-------------+-------+-------+---------------+--------------- +---------+------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+--------------- +---------+------+------+--------------------------+ | 1 | S...

using autocad using C
hi, im trying to draw some rectangles,lines in autocad using c, is their any function that can be used, plz help, also is it possible to read values from a ms excel file in c On Apr 3, 9:47 am, "Aman" <mehraama...@gmail.com> wrote: > hi, > im trying to draw some rectangles,lines in autocad using c, is their > any function that can be used, plz help, The details and techniques of creating or manipulating an interface between arbitrary C code and Autocad is out of the scope of this newsgroup. You would best ask this question in a newsgroup that relates to Autocad dev...

Disk in use when not in use
After a hang and hardware reset, I could not boot into eCS 1.14, getting a display that I've never seen before: a VGA display with a white background and a box that said the program pointed to by SET RUNWORKPLACE= could not be loaded. C-A-D worked to shut this display down, but reboot produced the same outcome. This was corrected by rebooting and using an archived desktop. To make sure everything is OK I next booted into my maintenance partition and tried to run CDKDSK C: /F. It wouldn't run because the disk was "in use or locked by another process". Excep...

file in use, could not use
Hi. I have two databases that are both linked to the same backend table. I can't use them both at the same time, yet other users can have both functioning simultaneously on their machines. Opening them is not the problem. ..but the error message appears when I run any queries linked to the table. It works in one database, but not the other. All users are running XP sp2 with Access 2003. The error message is the same regardless if anyone is using the backend or not. I believe it is machine specific...MY machine. I haven't a clue. Any ideas? -- Message posted via http://www.accessmon...

Use and usefulness of the as syntax
First, could you confirm the following syntax import foo as f equivalent to import foo f = foo Now, I was wondering about the usefulness in everyday programming of the as syntax within an import statement. Here are some instances retrieved from real code of such a syntax import numpy as np import math as _math import pickle as pickle -- In the first case, the syntax is motivated by brevity need, isn't it ? -- The second case seems to be rather widespread and causes math attribute to be private but I don't figure out why this matters. -- In the last case, I can see no poin...

To use locking or not to use it
Hello, I am builiding a multi-threaded application which has the following components : 1 writer thread - it gets information from the network and updates a table of numeric data, then informs the 2 reader threads that there is new information, so they should start running 2 reader threads that are awakened by the writer thread, use the writer thread's table to retrieve updated numerical data to do their calculations. Since the writer updates the table and the readers read from it, I want to use a reader/writer locking mechanism. However, I thought about the following...

Why index used/not used
Hello. Explain. I have table "traf_raw" contains field "sip_id" (integer). This field indexed with "CREATE INDEX traf_raw_sip ON traf_raw (sip_id)". Question. When I try to get different rows postgres use index with one "sip_id" and not use index with another "sip_id". I don't understand why it is happen, but with more complex queries Seq Scan is so slowly. Example. With "sip_id='19'" there many rows in table, with "sip_id='29'" there is no rows. cnupm=> ANALYZE traf_raw;...

Defined but not used? But I am using it!
I can't figure this out. So in a templated class of mine I have the following. namespace menutree { static bool debug = false; } And in various parts of the code I have statements like. if (menutree::debug) { std::cout << "here" << std::endl; Everything compiles but I get the warning "warning: `bool menutree::debug' defined but not used" Can someone shed some light on this? Thanks! Travis wrote: > I can't figure this out. So in a templated class of mine I have the > following. > > namespace menutree > { > static boo...

how to use the return value when using C# dlls in a C++ project?
hi, I am using a C# library in my C++ project. I want to make use of the classes defined in the library to do some job, and use the return value to go on other works in my project. But it seem difficult to convert a return value of C# datatype into C++ complaint datatype, like convert System::String in C# into std::string in C++. like in my test code: ClassA ^a = gcnew ClassA(); // ClassA is a class in the C# library System::String result = a-> getResult(); std::string str; // here I want to copy result to str, then I can use the string freely. But I don't know how to do it. Is th...

ActGen to use or not to use?
Hello, I was doing a project with Actel FPGA ex128 with Libero Platinum Eval version software. in my project I had to use some counters,Multiplexers etc. which I wrote myself. Now as I was a beginner I didnt use the ActGen. Is it worth the effort to write the code partly by using ActGen macros for counter and muxes and etc. Thanks Naimesh Naimesh wrote: > I was doing a project with Actel FPGA ex128 with Libero Platinum Eval > version software. > > in my project I had to use some counters,Multiplexers etc. which I > wrote myself. Good work. Writing your own code makes y...

To use or not to use CLOS
I must admit that I'm completely a newbie in Lisp, as I've been writing C and (some) C++ for years. I don't like the way in wich C++ resolves object programming, although sometimes I'm forced to use object oriented programming in C, in the same way as GTK+ does. Now I emprend a (personal) project and would like to start it from the very beginning in Lisp (my first project in this language). My simple question, and I want to ear answers from all you experts, is: do you recomend me CLOS or perhaps consider that using simple structure and Lisp facilities would be more than enough?...

Web resources about - Just When I Was Getting Used To Using Using - Arrgg - comp.lang.c++.moderated

Ted Cruz: Supreme Court Might 'Strike Down The Second Amendment' If Democrats Win In 2016
In an interview last week with conservative activist Michael Quinn Sullivan, Sen. Ted Cruz discussed the importance of the next president’s Supreme ...

Kanye West drops new song '30 Hours,' announces 'The Life of Pablo' download is coming
Soon, The Life of Pablo will finally come to life online. Kanye West announced that his seventh studio album will officially be released on ...

How to Have A Sexy Valentine's Day Even When It Is Fucking Freezing
Weather experts have predicted that a potentially historic cold snap will make its way across the East Coast and Ohio Valley this weekend, with ...

Villa Augustina School to close after nearly 100 years
A Catholic school in Goffstown will close its doors after nearly 100 years.

'He had no fear': Friends, brothers in arms remember officer killed in standoff
FARGO &mdash; Jason Moszer died Thursday with a fixed place in the memories of his friends and fellow police officers who knew him as a cop who ...

Report: Google planning a standalone VR headset
The Samsung Gear VR. The Wall Street Journal is reporting that Google is developing an "all-in-one" virtual reality headset that doesn't rely ...

Cameron Crowe’s ‘Roadies’ Drops Rockin’ First Trailer, Release Date
Showtime has peeled the stage curtain back on Cameron Crowe ‘s concert tour series “Roadies,” dropping its first trailer and a debut date on ...

AT&T testing superfast 5G mobile network
AT&T announced plans Friday to begin testing a superfast fifth generation, or 5G, mobile network with speeds up to 100 times faster than the ...

LA’s Aliso Canyon methane gas leak has been plugged – for now
The Southern California Gas Company announced yesterday that the massive methane leak plaguing the suburbs of Los Angeles has finally been plugged ...

What it’s really like to fly in ‘zero gravity’ like Ok Go did in their new music video
In case you missed it, Ok Go's new music video is literally out of this world. Ok, not literally, but it was shot in "zero gravity." The pop ...

Resources last updated: 2/12/2016 6:39:30 PM