How to name the namespace of my new project? - c++

I'm going to create a new library that renders genome annotations into charts. However, as C++ doesn't have a centralized library website like Perl, how do I know if the namespace conflicts with any existing one?

No, xd is not a good name: it's too short. It could be a good alias in a limited context, but for a library that will be used by others, provide a long, descriptive name. Then users can select an alias that makes sense for their project.
namespace my_company {
namespace XnoDraw {
// ...
} // namespace XnoDraw
} // namespace my_company
// user code, not your code:
namespace xd = my_company::XnoDraw;

You could use anything except std.
Note that c++ doesn't allow compound names for namespaces.
For instance:
// Allowed
namespace a
{
namespace b
{
int c;
}
}
// Not allowed
namespace a::b
{
int c;
}

Related

How to block using a C++ namespace [duplicate]

One of the vagaries of my development system (Codegear C++Builder) is that some of the auto-generated headers insist on having...
using namespace xyzzy
...statements in them, which impact on my code when I least want or expect it.
Is there a way I can somehow cancel/override a previous "using" statement to avoid this.
Maybe...
unusing namespace xyzzy;
Nope. But there's a potential solution: if you enclose your include directive in a namespace of its own, like this...
namespace codegear {
#include "codegear_header.h"
} // namespace codegear
...then the effects of any using directives within that header are neutralized.
That might be problematic in some cases. That's why every C++ style guide strongly recommends not putting a "using namespace" directive in a header file.
No you can't unuse a namespace. The only thing you can do is putting the using namespace-statement a block to limit it's scope.
Example:
{
using namespace xyzzy;
} // stop using namespace xyzzy here
Maybe you can change the template which is used of your auto-generated headers.
You may be stuck using explicit namespaces on conflicts:
string x; // Doesn't work due to conflicting declarations
::string y; // use the class from the global namespace
std::string z; // use the string class from the std namespace
For future reference : since the XE version there is a new value that you can #define to avoid the dreaded using namespace System; int the include : DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE
How about using sed, perl or some other command-line tool as part of your build process to modify the generated headers after they are generated but before they are used?
Quick experiment with Visual Studio 2005 shows that you can enclose those headers in your own named namespace and then use what you need from this namespace (but don't use the whole namespace, as it will introduces the namespace you want to hide.
#include<iostream>
#include<stdio.h>
namespace namespace1 {
int t = 10;
}
namespace namespace2 {
int t = 20;
}
int main() {
using namespace namespace1;
printf("%d" , t);
printf("%d" , namespace2::t);
}

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;
}

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

C++ Namespace question

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.

How can I "unuse" a namespace?

One of the vagaries of my development system (Codegear C++Builder) is that some of the auto-generated headers insist on having...
using namespace xyzzy
...statements in them, which impact on my code when I least want or expect it.
Is there a way I can somehow cancel/override a previous "using" statement to avoid this.
Maybe...
unusing namespace xyzzy;
Nope. But there's a potential solution: if you enclose your include directive in a namespace of its own, like this...
namespace codegear {
#include "codegear_header.h"
} // namespace codegear
...then the effects of any using directives within that header are neutralized.
That might be problematic in some cases. That's why every C++ style guide strongly recommends not putting a "using namespace" directive in a header file.
No you can't unuse a namespace. The only thing you can do is putting the using namespace-statement a block to limit it's scope.
Example:
{
using namespace xyzzy;
} // stop using namespace xyzzy here
Maybe you can change the template which is used of your auto-generated headers.
You may be stuck using explicit namespaces on conflicts:
string x; // Doesn't work due to conflicting declarations
::string y; // use the class from the global namespace
std::string z; // use the string class from the std namespace
For future reference : since the XE version there is a new value that you can #define to avoid the dreaded using namespace System; int the include : DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE
How about using sed, perl or some other command-line tool as part of your build process to modify the generated headers after they are generated but before they are used?
Quick experiment with Visual Studio 2005 shows that you can enclose those headers in your own named namespace and then use what you need from this namespace (but don't use the whole namespace, as it will introduces the namespace you want to hide.
#include<iostream>
#include<stdio.h>
namespace namespace1 {
int t = 10;
}
namespace namespace2 {
int t = 20;
}
int main() {
using namespace namespace1;
printf("%d" , t);
printf("%d" , namespace2::t);
}