Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Namespaces good for anything else?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 Nausea   Members   -  Reputation: 258

Like
0Likes
Like

Posted 03 December 2012 - 05:16 PM

Hi.

So I been wondering if namespaces is good for anything else but preventing name collisions?
For example: Using a namespace to make it clearer what belongs to a game engine.

If you can tell me any other uses, please do.

Thanks

Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 03 December 2012 - 05:37 PM

As far as I know, not really, but naming collisions can be a bigger deal than you might realize. For example, if you have a private free function in your engine named loadFile(), and a user of your engine also makes their own private free function named loadFile(), and their symbols would collide in the linking stage (assuming their signatures matched), even though the user never included any header from your engine that said anything about loadFile(). Using namespaces would be one way to prevent these symbols from colliding while still allowing multiple translation units (source files) to use the functions, which you should do so that end users can be happy.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 Hodgman   Moderators   -  Reputation: 32049

Like
3Likes
Like

Posted 03 December 2012 - 05:54 PM

I use a namespace called 'internal' to hide stuff that needs to be in a public header, but shouldn't be directly used by the API user.

I always put enums in a namespace, and then call the enum itself 'Type' so that their usage looks like:
MyOptions::Type var = MyOptions::Choice1;

If you have some functions/variables that need to be private to a single CPP file (I.e. can't be accessed with extern, etc) you can put them in an anonymous namespace.

#4 frob   Moderators   -  Reputation: 22837

Like
3Likes
Like

Posted 03 December 2012 - 06:06 PM

Namespaces can be useful lines of demarcation between subsystems. We use that all the time on our games.

They can also be useful to put standalone functions in a hierarchy, especially those functions that don't really belong to a class. It is a much better approach than some languages that require static functions in a utility class.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#5 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 03 December 2012 - 06:37 PM

I always put enums in a namespace, and then call the enum itself 'Type' so that their usage looks like:
MyOptions::Type var = MyOptions::Choice1;

Don't we now have strongly typed enum?
An invisible text.

#6 Hodgman   Moderators   -  Reputation: 32049

Like
2Likes
Like

Posted 03 December 2012 - 06:50 PM

Don't we now have strongly typed enum?

Yes, C++11's "enum class" is a big improvement that fixes several issues with C++'s "enum". The "namespace/struct wrapper around enum" pattern isn't necessary if you use enum class.

However, I'm still supporting C++03 compilers, so I'd rather not use C++11 code where it's not necessary.

#7 SiCrane   Moderators   -  Reputation: 9676

Like
0Likes
Like

Posted 03 December 2012 - 08:42 PM

If you have some functions/variables that need to be private to a single CPP file (I.e. can't be accessed with extern, etc) you can put them in an anonymous namespace.

I like the idea of anonymous namespaces but I dislike the way they interact with most tools, often including the IDEs I use. Basically, anonymous namespaces are usually implemented by creating a namespace with a pseudo-randomized name, so they make the actual symbol names really long and it just gets annoying dealing with the outputs of things like stack traces and profiler runs. Especially since the long randomized name is specific to a translation unit, and the profilers and debuggers already display file names for symbols. In theory it's useful if you do something like stick an anonymous namespace in a header, but since none of the symbol demanglers I've run across will get you to the translation unit from the anonymous namespace tag, it's pretty pointless (well maybe they do, but I've given up on anonymous namespaces long enough ago that I don't know it's currently a feature).

#8 BitMaster   Crossbones+   -  Reputation: 4443

Like
0Likes
Like

Posted 04 December 2012 - 02:36 AM

Do you remember which compilers those were? I just tried it in MSVC 2008 and the stack trace shows
>	createproxy-d.exe!`anonymous namespace'::locateGDALDataInternal()  Line 61	C++
Unfortunately I do not have other MSVC versions or Clang/gcc around to test this more thoroughly.

#9 NightCreature83   Crossbones+   -  Reputation: 3038

Like
0Likes
Like

Posted 04 December 2012 - 02:42 AM

Namespaces can be useful lines of demarcation between subsystems. We use that all the time on our games.

They can also be useful to put standalone functions in a hierarchy, especially those functions that don't really belong to a class. It is a much better approach than some languages that require static functions in a utility class.

But those languages often don't support standalone functions anyway and thus you have to use the static method function clutch because of that. In languages that do support standalone functions the file they are in is often the module that achieves the same semantics as namespaces in C++.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#10 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 04 December 2012 - 02:56 AM

Beside avoiding name conflicting, I often use namespace for,

1, Replace "static" keyword with unnamed namespace in source file.
2, Hide private/internal symbol in the header file.
namespace _internal { class MyInternal {}; }
_internal is still visible to others, but with the name, the others know it's for internal usage.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#11 rnlf   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 04 December 2012 - 06:13 AM

2, Hide private/internal symbol in the header file.
namespace _internal { class MyInternal {}; }
_internal is still visible to others, but with the name, the others know it's for internal usage.


You should consider using another name for this. Global names beginning with an underscore are reserved for the compiler. This might cause you some problems someday...

my blog (German)


#12 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 04 December 2012 - 06:18 AM

You should consider using another name for this. Global names beginning with an underscore are reserved for the compiler. This might cause you some problems someday...

yeah, that is for example.
my real code uses sub system name as prefix, such as foo_internal.
also, i think single underscore is safe, double underscore is reserved for comipler?

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#13 BitMaster   Crossbones+   -  Reputation: 4443

Like
0Likes
Like

Posted 04 December 2012 - 06:27 AM

According to this Stack Overflow post, _internal should be safe unless it is in the global namespace.

#14 rnlf   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 04 December 2012 - 08:42 AM

It could also mean that there is a global name _internal which might produce some ambiguities. But foo_internal is a completely different story and should be perfectly safe.

my blog (German)


#15 BitMaster   Crossbones+   -  Reputation: 4443

Like
0Likes
Like

Posted 04 December 2012 - 09:22 AM

Well, the wording in the standard appears to be explicitly "Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace." which is quite distinct from the order of words chosen for anything with a double underscore or underscore plus uppercase letter which are "reserved to the implementation for any use". The standard went quite a bit out of the way to explain where it is forbidden (the global namespace only).
So a standard compliant implementation cannot really use anything that would cause ambiguities with something like
namespace myname {
   namespace _internal {
      // ...
   }
}
Anything not respecting the scoping rules here would have to be named with a double underscore or underscore plus uppercase letter.

While I would personally avoid _internal as a personal preference, remember there is a whole coding convention built around _name for member variables and name_ for parameters (I don't like it either, but sporadically you encounter it). Without the global namespace only rule, that would lead to quite possible name clashes.

#16 Servant of the Lord   Crossbones+   -  Reputation: 21217

Like
0Likes
Like

Posted 04 December 2012 - 09:34 AM

For example: Using a namespace to make it clearer what belongs to a game engine.

Yes, namespaces are definitely useful for telling you which subsystem or library a class belongs in.

For example, I have Common:: (non-specific code - my general code collection), Engine:: (genre-specific code), Game:: (game-specific code).
I also have Engine::World:: and Common::Input:: and other such embedded namespaces to contain classes or functions that go together in a collection.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#17 monkeyboi   Members   -  Reputation: 188

Like
0Likes
Like

Posted 04 December 2012 - 10:36 AM

1. You can easily add anything into any namespace except std without looking into that namespace
2. Not quite sure about this one but I think it is worthy to mention tho. Looking up machanism, if your caller's parameter is in a namespace the compiler will first try to find the most matched function in that namespace then globle space.

#18 Neilo   Members   -  Reputation: 290

Like
0Likes
Like

Posted 04 December 2012 - 11:07 AM

I don't really like seeing namespaces to break down subsystems in C++ projects. Similarly, I don't think there is a need for nested namespaces.

That said, I use a single top level namespace for each project I work on and heavy use of anonymous namespaces for implementation hiding.

#19 Bregma   Crossbones+   -  Reputation: 5505

Like
1Likes
Like

Posted 04 December 2012 - 11:17 AM

Just for the record, C++ namespaces can also be used to perform compile-time selection of alternate implementations. You don't see it very often, but when it's needed it's very useful.
Stephen M. Webb
Professional Free Software Developer

#20 Yrjö P.   Crossbones+   -  Reputation: 1412

Like
0Likes
Like

Posted 04 December 2012 - 01:51 PM

I don't really like seeing namespaces to break down subsystems in C++ projects. Similarly, I don't think there is a need for nested namespaces.

Nested namespaces might make sense when you are writing a library and want to prevent accidental argument-dependent lookups? You could have your public datatypes and anything you intend to offer via ADL in the main namespace, and anything you want to be explicitly accessible but hidden from ADL would go in a nested namespace.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS