Effects of improper/different namespace usage.

Jones
Unregistered
 
Post: #1
Take a gander at this little section of code.

Code:
// function.h

namespace my {
void func();
};

Code:
// function.cpp

using namespace my;

void func() {
// do stuff
}

Lets say those two files were destined to be compiled into a framework/static library with headers. Would anything be wrong? Would it be *better* in any way to wrap normal namespace braces around the definition (not declaration) of the function?

Cplusplus dot com had no opinion on the subject.

Your input would be appreciated.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Jones Wrote:Lets say those two files were destined to be compiled into a framework/static library with headers. Would anything be wrong?

I don't think so, but I guess it's possible that the definition of myfunc might not be of my::myfunc, which would be wrong.

Quote:Would it be *better* in any way to wrap normal namespace braces around the definition (not declaration) of the function?

Yes. It would avoid the potential confusion I had above, if nothing else Wink

You should never use "using namespace", and very very seldom use "using" at all.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #3
cplusplus.com may have had no opinion, but in my experience it's best to be explicit. Wrap your implementation in the namespace just like you wrap your declaration. Leave as little ambiguity to the compiler as possible.
Quote this message in a reply
Jones
Unregistered
 
Post: #4
Thanks guys, I figured as much. Smile

@OneSadCookie:

What's wrong with using? You need it to take advantage of some great stuff like strings and other STL classes.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #5
It's probably best to not use using, but in some cases, like with std, it can make things a lot more tedious. However, one case where namespaces with using were causing me trouble was when I was attempting to compile a version of ODE with Opcode. It happened to use using with all their namespaces, one of which used a class that was already taken. (I believe it was Rect) I spent hours getting it to compile by adding namespace resolutions. Of course, I now realize all I probably had to do was get rid of the pre-compiled header that's always produced with your project so it wouldn't have the Carbon headers included. (in fact I have since removed it, and there is now a new version of ODE, so I think I'll test it now. Rasp) But still, it provides a problem.

Edit: it does indeed work now. Yay, I won't have to edit for hours whenever there's an update! Rasp
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Jones Wrote:What's wrong with using? You need it to take advantage of some great stuff like strings and other STL classes.

Nonsense. Just scope the names properly -- std::string, std::vector, etc.
Quote this message in a reply
Jones
Unregistered
 
Post: #7
OneSadCookie Wrote:Nonsense. Just scope the names properly -- std::string, std::vector, etc.

That's not what I meant. You need to add the line of code:

Code:
using std::string;

For strings to work at all. xCode rejects them unless I add that line of code.
Quote this message in a reply
Jones
Unregistered
 
Post: #8
edit

Nevermind.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #9
In my experience, the 'using' keyword is perfectly safe, just keep it out of your headers. Personally, I only ever use 'using' in my method blocks.

E.g.

Code:
void SomeClass::someMethod( void )
{
     using namespace foo;
     ...
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Trouble With NSDate Usage Xenos 11 5,200 Sep 11, 2006 05:45 AM
Last Post: Xenos
  In-Game Memory Usage Info Griggs 1 3,184 Sep 5, 2002 09:12 PM
Last Post: zzajin