Mysteries of Namespaces
Michael_Wong 120000M1EH Comments (6) Visits (9101)
There are few features added in the last C++98 standardization that caused nearly all earlier C++ code to break. One is the addition of namespace and the moving of almost all of the C++ Standard library into it. Can you name a few examples that were not moved into standard namespace? This is the first Mystery of the Month.
Yet, used in its simplest way, namespaces are basically benign. A namespace is a subdivision of global scope. But even basic namespace as they are come with gotchas, which I will show in this discussion.
Next, we will take a closer look at using directives and declarations.
There are a number of key differences between these two. What are they and can you figure it out from the way this code behaves:
Call this the second Mystery of the Month. What is this code telling us about the real nature of these two kinds of usings (specifically, about where they are actually declared)?
Knowing this, it saids something about their usage style too. IN fact, is it really a good choice to use them the way I have,
1. inside a function body or
2. should I place them at file scope ahead of the include or
3. should I place them at file scope after the include or
4. should I place them in a header file
This is mystery number 3. Where should they be used and this one is necessarily somewhat subjective in one of the choice, but one is clearly evil. You just need to defend your choices.