#2 Moderator* - Reputation: 5362
Posted 03 December 2012 - 05:37 PM
#3 Moderators - Reputation: 13533
Posted 03 December 2012 - 05:54 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;
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 Moderators - Reputation: 7623
Posted 03 December 2012 - 06:06 PM
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.
#6 Moderators - Reputation: 13533
Posted 03 December 2012 - 06:50 PM
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.Don't we now have strongly typed enum?
However, I'm still supporting C++03 compilers, so I'd rather not use C++11 code where it's not necessary.
#7 Moderators - Reputation: 6640
Posted 03 December 2012 - 08:42 PM
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).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.
#8 Members - Reputation: 1397
Posted 04 December 2012 - 02:36 AM
> 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 Crossbones+ - Reputation: 1154
Posted 04 December 2012 - 02:42 AM
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++.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.
#10 Members - Reputation: 682
Posted 04 December 2012 - 02:56 AM
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 Members - Reputation: 725
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...
#12 Members - Reputation: 682
Posted 04 December 2012 - 06:18 AM
yeah, that is for example.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 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 Members - Reputation: 1397
Posted 04 December 2012 - 06:27 AM
#15 Members - Reputation: 1397
Posted 04 December 2012 - 09:22 AM
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 Marketplace Seller - Reputation: 8937
Posted 04 December 2012 - 09:34 AM
Yes, namespaces are definitely useful for telling you which subsystem or library a class belongs in.For example: Using a namespace to make it clearer what belongs to a game engine.
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.
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
#17 Members - Reputation: 188
Posted 04 December 2012 - 10:36 AM
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 Members - Reputation: 284
Posted 04 December 2012 - 11:07 AM
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 Members - Reputation: 2762
Posted 04 December 2012 - 11:17 AM
Professional Free Software Developer
#20 Crossbones+ - Reputation: 1037
Posted 04 December 2012 - 01:51 PM
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.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.







