Public Group

# Namespace declaration in C++ header file.

This topic is 2788 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The language used is entirely C/C++.

While running around in Google search, I came upon this. (Source link here.)

Namespace Rule #2: Never write namespace using declarations in header files.Note that Rule #2 goes much further than most popular advice. Most authors recommend that using declarations never appear at file scope in shared header files. That's good advice, as far as it goes, because at file scope a using declaration causes the same kind of namespace pollution as a using directive, only less of it.

<p class="MsoNormal">Unfortunately, in my opinion the above "popular advice" doesn't go far enough. Here is why you should never write using declarations in header files at all, not even in a namespace scope: The meaning of the using declaration may change depending on what other headers happen to be #included before it in any given module. This kind of unpredictability is bad, and should never be permitted. I'll illustrate this problem later in Example 2©.

[/quote]

Does it mean this is not allowed in the header file?

[source lang="cpp"]
#ifndef MAIN_H
# define MAIN_H
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif

#include <Windows.h>
#include <D3D10.h>
#include <D3DX10.h>

//First, a namespace.
namespace Core
{
void CreateOneAndOnlyWindow();
};

#endif //End of MAIN_H
[/source]

##### Share on other sites
No, it means that this is not allowed://File widget.h #include "main.h" using namespace Core;//ahh, you used "using namespace" in a header!! //...

##### Share on other sites

The language used is entirely C/C++.

Which language is it? Is it entirely C, or entirely C++? You might have well said, "The language is entirely Java/List/Pascal".
C is not C++. C++ is not C. There is no language called C/C++, and you cannot write 'entirely' in two different languages at once.
I'm nitpicking, since obviously if you are talking about namespaces it's C++, but it's an important distinction.
Some people, when thinking of C++ as an updating C, tend to misuse C++ and not take proper advantage of it's features.

Does it mean this is not allowed in the header file?
[/quote]

No, it means this:
//My header file. using namespace Core;
...is allowed by C++, but bad programming practice.

It sounds to me like he's just saying, "Don't use the keyword 'using' in a header file" which is sound advice to me.

I don't know what you mean by the function "Calling_namespace_in_a_header()", though.

##### Share on other sites
The reason why you don't want to use "using namespace" in header files is because two different namespaces may contain elements with the same name (namespace1::class1 and namespace2::class1). And if you accidentally include those namespaces together it won't be clear from which namespace the element should be taken or you might end up accessing the wrong one.

The proper way of using namespaces is accessing their elements preceding them by their proper namespace in the header file and, for convenience (if really necessary), using the "using namespace" in the cpp.

##### Share on other sites
Ah, so then I could wrap main() inside the namespace and be done with it.

And I thought some weird usage of namespace in the header file would bring the critics down upon me... when all this time, the article meant the keyword "using"...

##### Share on other sites

Ah, so then I could wrap main() inside the namespace and be done with it.

If I understand your intention correctly then you're wrong. A function inside namespace(s) gets a name composed of the wrapping namespace(s) and the function name. Hence a main() inside a namespace would no longer be found by the symbol _main, and hence the linker would complain that the program were not complete ("missing symbol _main"). Here main() is a special case because it is the one function that defines the transition point from the runtime code to the custom code and hence is a necessity to be known a-priori by the runtime. (But you could tell the linker what the new "main" is, too.)

##### Share on other sites

If I understand your intention correctly then you're wrong. A function inside namespace(s) gets a name composed of the wrapping namespace(s) and the function name. Hence a main() inside a namespace would no longer be found by the symbol _main, and hence the linker would complain that the program were not complete ("missing symbol _main"). Here main() is a special case because it is the one function that defines the transition point from the runtime code to the custom code and hence is a necessity to be known a-priori by the runtime. (But you could tell the linker what the new "main" is, too.)

Ah, that concludes my question. Thanks again.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013600
×