C++ Namespace question - c++

I am working on some code written by a co-worker who no longer works with the company, and I have found the following code: (which I have cut down below)
namespace NsA { namespace NsB { namespace NsC {
namespace {
class A { /*etc*/ };
class B { /*etc*/ };
}
namespace {
class C { /*etc*/ };
}
} } }
I don't understand the purpose of the namespace commands on lines 3 and 8.
Can someone explain what the purpose of an namespace entry with no name is?
Thanks

That's an "anonymous namespace" - which creates a hidden namespace name that is guaranteed to be unique per "translation unit" (i.e. per CPP file).
This effectively means that all items inside that namespace are hidden from outside that compilation unit. They can only be used in that same file. See also this article on unnamed namespaces.

Related

Understanding TOTW 153 example with namespace polution

In TOTW 153 it is claimed that using namespace in function scope can leak in the global scope,
in other words they claim this is:
namespace totw {
namespace example {
namespace {
TEST(MyTest, UsesUsingDirectives) {
using namespace ::testing;
Sequence seq; // ::testing::Sequence
WallTimer timer; // ::WallTimer
}
} // namespace
} // namespace example
} // namespace totw
roughly equivalent to:
using ::testing::Expectation;
using ::testing::Sequence;
using ::testing::UnorderedElementsAre;
...
// many, many more symbols are injected into the global namespace
namespace totw {
namespace example {
namespace {
TEST(MyTest, UsesUsingDirectives) {
Sequence seq; // ::testing::Sequence
WallTimer timer; // ::WallTimer
...
}
} // namespace
} // namespace example
} // namespace totw
So I expected that if I do this it will compile:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
namespace totw {
namespace example {
namespace {
TEST(MyTest, UsesUsingDirectives) {
using namespace ::testing;
Sequence seq; // ::testing::Sequence
}
} // namespace
} // namespace example
} // namespace totw
Sequence s; //notice no testing::
It does not. So I wonder if I am wrong in replicating the example, or is the TOTW153 example misleading?
My best guess is that their example is "wrong" in a sense that what they claim is roughly equivalent is not really equivalent in a sense I would assume it is.
You are correct that the "transpiled" code could lead one to think that your last snippet should work. But that's not what they meant.
The using ::testing::Expectation etc. are added to the global namespace from the perspective of the TEST scope. In other words, the transpiled code is effectively identical to the original only within the TEST scope. Outside of that scope there is no effect, but the problems they list are still problems (unless you know that neither the TEST scope contents nor the contents of any involved namespaces will ever be changed). In other words, the using introduces potential collisions between all (present and future) symbols in that TEST scope and the symbols (present and future) in all the involved namespaces. Sounds less scary but will still bite you in the long run.

How to extend an assigned namespace

I want to explain my question over an instance. I am using an third party library, having its own namespace. I want to import a part of this library, having its own namespace inside the namespace mentioned below.
namespace library {
namespace part {
}
}
There is also a hierarchy in the current project. I want to import and extend the library::part inside to my project with another name. I try to do as below:
#include <library/part>
namespace project {
namespace my_part = library::part;
}
namespace project {
namespace my_part {
void my_extension_1();
void my_extension_2();
}
}
The scenario can be done with the current tools of the language? If not, how should a workaround can be done? Still not, why?
Edit: Error message gcc 5.3.0 dumps:
error: conflicting declaration of namespace ‘project::my_part’
Edit: There is a suggestion about extending the original namespace, but that I am asking for. library::part users should not have a direct access to the functions I have added.
I believe you want to make a namespace of your own that contains
everything that library::part contains, and more besides, without
putting anything more into library::part. Like this?
namespace library {
namespace part {
const int library_part_i = 123;
}
}
namespace project {
namespace part {
using namespace library::part;
void my_extension_1(){};
void my_extension_2(){};
}
}
int main()
{
// const int i = library_part_i; <- Does not compile
// const int i = project::library_part_i; <- Does not compile
const int i = project::part::library_part_i;
// library::part::my_extension_1(); <- Does not compile
project::part::my_extension_2();
return 0;
}

Is it ok to wrap code in anonymous namespaces for using directives? [duplicate]

This question already has answers here:
Is using namespace in an anonymous namespace safe?
(2 answers)
Closed 3 months ago.
I want using namespace std; to apply to classes as well as functions without polluting the global namespace, but I'm wondering if it's an ok approach.
namespace
{
using namespace std;
class S
{
public:
S()
{
cout << "ok";
}
friend ostream operator<<(ostream& os, const S& s);
};
}
Any caveats to this?
It will work but keep in mind the following points:
You should limit its use in a source file, not in a header file (in general you should refrain from using unnamed namespaces in headers since they can easily mess around with your symbol definitions, especially if there are inline functions that use something from the anonymous namespace).
It's a bad practice and adding an additional naming hierarchy layer (i.e. the anonymous namespace) just for laziness is as bad as it sounds.
If it's in header file then it's not preferable as this file could be included in multiple source file. If in some source file then its acceptable
Whilst this looked like a great solution, in my experiments it doesn't do as I expected it would. It doesn't limit the scope of the using directive.
Consider this:
#include <string>
namespace {
string s1; // compile error
}
namespace {
string s2; // compile error
}
string s3; // compile error
int main()
{
}
None of those strings compile because they are not properly qualified. This is what we expect.
Then consider this:
#include <string>
namespace {
using namespace std;
string s1; // compiles fine (as expected)
}
namespace {
string t2; // compiles fine (I didn't expect that)
}
string v3; // compiles fine (I didn't expect that either)
int main()
{
}
So placing a using directive within an unnamed namespace appears to be exactly the same as placing it in the global namespace.
EDIT: Actually, placing a symbol in an unnamed namespace makes it local to the translation unit. So this is why it can't work for the intended purpose.
Therefore it has to be a no-no for headers.
An unnamed namespace doesn't contain the effects of a using-directive:
namespace A {int i;}
namespace {
using namespace A;
}
int j=i,k=::i; // OK
Both qualified and unqualified lookup follow the implicit using-directive for the unnamed namespace and then follow the explicit one within it. Wrapping the unnamed namespace inside another namespace limits the unqualified case to other code within that outer namespace, of course:
namespace A {int i;}
namespace B {
namespace {
using namespace A;
}
int j=i; // OK, as above
}
int j=i, // error: i not found
k=B::i; // OK
However, in either case you might as well write the using-directive outside the unnamed namespace (or not write it at all if it would be problematic, perhaps because it would appear in a header file).
Don't take the decision to use an anonymous namespace just so you can limit the scope of a using directive.
That being said, if an anonymous (or any other) namespace is already there and you want the advantages of the using inside it, it's fine as long as your coding standards okay it.

"using namespace" statement inside an anonymous namespace

When using a using namespace statement inside an anonymous namespace bring the namespace used in to the file scope? Eg:
namespace foo
{
int f() { return 1; }
}
namespace
{
using namespace foo;
}
int a()
{
return f(); // Will this compile?
}
According to 7.3.4 [namespace.udir] paragraph 4 a namespace directive is transitive:
For unqualified lookup nominates a second namespace that itself contains using-directives, the effect is as if the using-directives from the second namespace also appeared in the first.
... and according to 7.3.1.1 [namespace.unnamed] paragraph 1 there is kind of an implicit using directive for the unnamed namespace:
An unnamed-namespace-definition behaves as if it were replaced by
inline namespace unique { /* empty body */ }
using namespace unique ;
namespace unique { namespace-body }
where inline appears if and only if it appears in the unnamed-namespace-definition, all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the entire program.
Thus, the answer is "yes, this is supposed to compile" (and it does with all C++ compilers I tried it with).
Yes.
This is because an anonymous namespace is automatically brought into the containing scope.
Yes, because, as Dietmar Kühl quoted, an anonymous namespace is replaced by its content.
However, you should pay attention that it is replaced exactly where it is declared (edit), so there is no "magic" in this. For example, this won't work:
namespace foo
{
int f() { return 1; }
}
int a()
{
return f(); // Will this compile?
}
namespace
{
using namespace foo;
}

Can I undo the effect of "using namespace" in C++?

With using namespace I make the whole contents of that namespace directly visible without using the namespace qualifier. This can cause problems if using namespace occurs in widely used headers - we can unintendedly make two namespaces with identical classes names visible and the compiler will refuse to compile unless the class name is prepended with the namespace qualifier.
Can I undo using namespace so that the compiler forgets that it saw it previously?
No, but you can tell your coworkers that you should never have a using directive or declaration in a header.
As others said, you can't and the problem shouldn't be there in the first place.
The next-best thing you can do is bring in your needed symbols so that they are preferred by the name look-up:
namespace A { class C {}; }
namespace B { class C {}; }
using namespace A;
using namespace B;
namespace D {
using A::C; // fixes ambiguity
C c;
}
In some cases you can also wrap the offending includes with a namespace:
namespace offender {
# include "offender.h"
}
No, C++ Standard doesn't say anything about "undo". The best you are allowed to do is to limit scope of using:
#include <vector>
namespace Ximpl {
using namespace std;
vector<int> x;
}
vector<int> z; // error. should be std::vector<int>
But unfortunately using namespace Ximpl will bring all names from std namespace as well.
Not to my knowledge... But as a rule I only use "using namespace" in .cpp files.
The closest, that I'll try to use in header files is following:
//example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
/**
* hating c++ for not having "undo" of using namespace xx
*/
#define string std::string
#define map std::map
class Example {
public:
Example (const char *filename);
Example (string filename);
~Example ();
private:
map<string,complicated_stuff*> my_complicated_map;
};
#undef string
#undef map
#endif //EXAMPLE_H_
after all, defines are #undef -able.
There are 2 problems:
1. it is ugly
2. separate #define and #undef for each name from the corresponding namespace are used
As stated you should not use using namespace sth in header files. When you need functionality from a namespace in your implementation you can leverage scopes like this:
void func() {
// some code agnostic to your namespace.
{
using namespace sth;
// some code aware of sth.
}
// some other code agnostic to your namespace.
}