C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why should this compile?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Maxim
Guest





PostPosted: Thu Jul 29, 2004 3:21 pm    Post subject: why should this compile? Reply with quote



I have some code in a .h file that declares a class within a
namespace. The .cpp file defines the class functions, but not inside
the namespace - only a namespace declaration is used. I thought the
code was crazy, but it compiled on vc71 and gcc - can anyone please
explain 1) is this standard 2) why this is bad style (for one it
confuses the heck out of other programmers).
thanks,
max,

// .h file
namespace TestNamespace
{
struct StupidTest
{
void foo();
};
}

// .cpp
using namespace TestNamespace;

void StupidTest::foo()
{
std::cout<<"okn";
}

int main(int argc, char* argv[])
{
StupidTest t;
t.foo();
return 0;
}

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





PostPosted: Fri Jul 30, 2004 9:38 am    Post subject: Re: why should this compile? Reply with quote



[email]maxk (AT) touro (DOT) edu[/email] (Maxim) wrote in message news:<a3e5c0e6.0407290611.11abdaa (AT) posting (DOT) google.com>...
Quote:
I have some code in a .h file that declares a class within a
namespace. The .cpp file defines the class functions, but not inside
the namespace - only a namespace declaration is used. I thought the
code was crazy, but it compiled on vc71 and gcc - can anyone please
explain 1) is this standard 2) why this is bad style (for one it
confuses the heck out of other programmers).

Your code is legal. Frequently, the 'using namespace' statement
leads to a lot of confusion and problems. As a small example, see
below:

using namespace std;
...
typedef ... map; // redefining std::map
...
#include <map> // you'll get a compiler error inside map

One of the reasons of choosing 'std' for the standard library
namespace is to allow avoidance of the statement 'using namespace std'
in your code.
In my code, I usually choose short namespace names, around 5-8
chars, and use special interface/session classes for using the code
from outside of the namespaces. I rarely use 'using namespace', and
only in controlled environment - small .cpp files, with comments
regarding the 'using' stmt.
If you want trouble, you are invited to put 'using namespace' in a
..h file!-)

I'm not sure that this is the right place to do style wars. If you
invent a better wheel, many of us will happily use it. But first I'd
like you to try it on your coworkers or even on yourself!-) A good
sanity check is to return to your code two months or more after
finishing it, and to do some significant changes.
I think that the only use of 'using namespace' is when you inherit
long namespaces, but this is my personal opinion, and YMMV.

You didn't state explicitly that the code is not yours, and I assume
that you returned to your old code and thought that it was crazy. It
is not crazy, and it is usable in some circumstances.
If the code is not yours, and the purpose of your post is to
convince others to use better style, then I doubt that you would
succeed. Changing coding style is not easier than changing speaking
or writing style. And calling somebody other's code 'Stupid' wouldn't
help in this case.

Quote:
// .h file
namespace TestNamespace
{
struct StupidTest
{
void foo();
};
}

// .cpp

#include "...h"

Quote:
using namespace TestNamespace;

void StupidTest::foo()
{
std::cout<<"okn";
}

#ifdef SIMPLE_FOO_TEST
// I'd prefer the implementation of Test being in a separate file,
// not with your standard main

Quote:
int main(int argc, char* argv[])
{
StupidTest t;
t.foo();
return 0;
}

#endif

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

Back to top
Maxim Yegorushkin
Guest





PostPosted: Fri Jul 30, 2004 3:21 pm    Post subject: Re: why should this compile? Reply with quote



Maxim <maxk (AT) touro (DOT) edu> wrote:

Quote:
I have some code in a .h file that declares a class within a
namespace. The .cpp file defines the class functions, but not inside
the namespace - only a namespace declaration is used. I thought the
code was crazy, but it compiled on vc71 and gcc - can anyone please
explain 1) is this standard

Yes, it is. 'using namespace' (using directive) makes all the names from that namespace immediately accessible from the current scope without qualification, that is in your .cpp code you can use the name StupidTest without explicitly qualifying it with its namespace.

Quote:
2) why this is bad style (for one it confuses the heck out of other programmers).

I would not say it's bad because I see no harm in judicious use of using directive in cpp's. I would rather say is's lazy.

--
Maxim Yegorushkin

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

Back to top
Mike Capp
Guest





PostPosted: Fri Jul 30, 2004 3:36 pm    Post subject: Re: why should this compile? Reply with quote

[email]maxk (AT) touro (DOT) edu[/email] (Maxim) wrote in message news:<a3e5c0e6.0407290611.11abdaa (AT) posting (DOT) google.com>...
Quote:
I have some code in a .h file that declares a class within a
namespace. The .cpp file defines the class functions, but not inside
the namespace - only a namespace declaration is used. I thought the
code was crazy, but it compiled on vc71 and gcc -

Remember that

void StupidTest::foo() { this->whatever(); }

will be compiled to something much like

void foo(StupidTest* this) { this->whatever(); }

As long as you tell the compiler somehow that the identifier
StupidTest refers to TestNamespace::StupidTest, it'll be happy.

Quote:
can anyone please
explain 1) is this standard

Yes, perfectly standard.

Quote:
2) why this is bad style (for one it
confuses the heck out of other programmers).

I don't think it's *particularly* bad style. The using-directive is
unnecessarily broad in scope - "using TestNamespace::StupidTest;"
would be cleaner - and personally I'd put the implementation into a
"namespace { ... }" block, but I wouldn't shoot someone for writing
it.

Cheers,
Mike

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

Back to top
Peter C. Chapin
Guest





PostPosted: Fri Jul 30, 2004 3:36 pm    Post subject: Re: why should this compile? Reply with quote

[email]tmk (AT) netvision (DOT) net.il[/email] (Michael Tiomkin) wrote in
news:ef72c6cb.0407291612.4549daa4 (AT) posting (DOT) google.com:

Quote:
I think that the only use of 'using namespace' is when you inherit
long namespaces, but this is my personal opinion, and YMMV.

Namespace aliases are good for situations like this too. I also find aliases
handy if I want to simplify the use of a "deeply" nested namespace without
throwing out namespace qualification entirely.

Peter

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

Back to top
Maxim
Guest





PostPosted: Sat Jul 31, 2004 3:08 am    Post subject: Re: why should this compile? Reply with quote

[email]tmk (AT) netvision (DOT) net.il[/email] (Michael Tiomkin) wrote in message
Quote:
I have some code in a .h file that declares a class within a
namespace. The .cpp file defines the class functions, but not inside
the namespace - only a namespace declaration is used. I thought the
code was crazy, but it compiled on vc71 and gcc - can anyone please
explain 1) is this standard 2) why this is bad style (for one it
confuses the heck out of other programmers).

Your code is legal.

Could you please explain why? As far as I see it member function
definitions have to be in the same namespace as the class, and in my
example they are in global namespace (and the TestNamespace is just
made visible by the 'using'). This is what I am interested at this
point. BTW the reason I called the class StupidTest because I could
not come up with anything better, I was not trying to insult the
original programmer. He himself agreed that this style is not standard
and changed it; I was left with some wonderment as to why the heck it
compiled in the 1st place.
max.

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.