• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nausea

Namespaces good for anything else?

20 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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 [font=courier new,courier,monospace]loadFile()[/font], and a user of your engine also makes their own private free function named [font=courier new,courier,monospace]loadFile()[/font], 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 [font=courier new,courier,monospace]loadFile()[/font]. 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.
0

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1354578876' post='5006840']
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;

[/quote]
Don't we now have strongly typed enum?
0

Share this post


Link to post
Share on other sites
[quote name='lride' timestamp='1354581430' post='5006862']Don't we now have strongly typed enum?[/quote]Yes, C++11's "[font=courier new,courier,monospace]enum class[/font]" is a big improvement that fixes several issues with C++'s "[font=courier new,courier,monospace]enum[/font]". The "[i][font=courier new,courier,monospace]namespace[/font]/[font=courier new,courier,monospace]struct[/font] wrapper around [font=courier new,courier,monospace]enum[/font][/i]" pattern isn't necessary if you use [font=courier new,courier,monospace]enum class[/font].

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

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1354578876' post='5006840']
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.
[/quote]
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).
0

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='frob' timestamp='1354579567' post='5006845']
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.
[/quote]
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++.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='wqking' timestamp='1354611390' post='5007014']
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.
[/quote]

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...
0

Share this post


Link to post
Share on other sites
[quote name='rnlf' timestamp='1354623187' post='5007047']
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...
[/quote]
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?
0

Share this post


Link to post
Share on other sites
According to this [url=http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier]Stack Overflow[/url] post, _internal should be safe unless it is in the global namespace.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
[code]namespace myname {
namespace _internal {
// ...
}
}[/code]
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.
0

Share this post


Link to post
Share on other sites
[quote name='Nausea' timestamp='1354576575' post='5006827']
For example: Using a namespace to make it clearer what belongs to a game engine.[/quote]
Yes, namespaces are definitely useful for telling you which subsystem or library a class belongs in.

For example, I have [b]Common::[/b] (non-specific code - my general code collection), [b]Engine::[/b] (genre-specific code), [b]Game::[/b] (game-specific code).
I also have [b]Engine::World::[/b] and [b]Common::Input::[/b] and other such embedded namespaces to contain classes or functions that go together in a collection.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[quote name='Neilo' timestamp='1354640864' post='5007129']
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.[/quote]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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0