How to extend an assigned namespace - c++

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

Related

Adding items to an aliased namespace

I'm using a namespace to switch between different versions of my database implementation. My client code should not need to know the details so I use a namespace alias to hide the specific version from the client code.
db_v1.h
namespace db_v1
{
class Database ...
}
db_def.h
#ifdef _DB_V1
#include "db_v1.h"
#endif
namespace db = db_v1;
Now if I want to extend the namespace with additional items, which are not version specific, I would like to add them to the namespace db, but the problem is that I can not use namespace db because it is an alias.
db_global.h
namespace db <-- should be using the namespace for the current version
{
typedef enum
{
OK
} value;
}
Obviously I get an error here because the namespace db already exists, while what I really want is, to extend the namespace without knowing wich version is the current one.
As far as I can see, I would have to put such a defintion into a separate namespace like db_global or I would have to duplicate such symbols in all versions, which I don't really like.
Is there some way to define it such that I can write in the client code something like:
x = db::value::OK;
Maybe
#ifdef _DB_V1
#include "db_v1.h"
#endif
namespace db {
using namespace db_v1;
}
in db_def.h instead of namespace db = db_v1;? This way all contents of db_v1 are imported into db namespace. Obviously, it may be conditionally-compiled:
namespace db {
#ifdef _DB_V1
using namespace db_v1;
#elif defined _DB_V2
using namespace db_v2;
#endif
}
For example, this code works well:
namespace db_v1 {
void foo(){}
}
namespace db_v2 {
void foo(){}
}
namespace db {
using namespace db_v1;
}
namespace db {
typedef enum
{
OK
} value;
}

How do I bring a single function into the current namespace?

Suppose I want to use the std::max function in my namespace A. How would I do this?
namespace A {
void fun()
{
double x = std::max(5.0, 1.0); // I don't want to have to write the std::
}
void fun()
{
using namespace std;
double x = max(5.0, 1.0); // I don't want to have to use the using directive to introduce the entire namespace
}
}
Is there a way of doing this?
You can "import" individual symbols by naming them in a using declaration:
namespace A
{
using std::max;
This means that A::max is defined and designates the same function as std::max; so attempts to look up max in namespace A will find the intended function.
(This is an Answer version of Brandon's comment on the original post)

How to name the namespace of my new project?

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

How to tell C++ to use a different function (with the same name as another function)?

I have two libraries included in my program which both have the same function name, but I need to be able to use both, but I also need C++ to know which one I'm referring to (in certain places I will only be referring to one or the other). The reason why I'm doing this is because I am making my own library and I want to have certain names for my functions, but they are conflicting with functions in someone else's library that I've included, and to make matters worse, some of my functions in my library actually USE the functions in the other persons library which has the same name.
My library is just a .h/.cpp file by the way. Also, when calling MY functions, I don't want any extra luggage such as myNameSpace::myFunc(). I just want to call it myFunc(). However, I don't mind calling the other persons function using a namespace (though I don't want to modify their library in case I break something). (I'm completely new to C++ btw)
HERES MY NEW (TEST - SO FAR) CODE : NOT WORKING W/ ERRORS:
error C2668: 'myFunc' : ambiguous call to overloaded function
main program.cpp
#include "otherslib.h"
#include "mylib.h"
#include <iostream>
using namespace myNamespace;
int main(){
std::cout << myFunc() << std::endl;
return 0;
}
mylib.h
#pragma once
namespace myNamespace{
int myFunc();
}
mylib.cpp
#include "mylib.h"
namespace myNamespace{
int myFunc(){
return 1;
}
}
otherslib.h
#pragma once
int myFunc();
otherslib.cpp
#include "otherslib.h"
int myFunc(){
return 0;
}
You should define your functions in a namespace, and use the namespace when calling them.
namespace myNamespace
{
int myFunc(etc) { ... }
}
int main() {
cout << myNamespace::myFunc();
}
To avoid having to specify your namespace all the time, you could do something like this:
namespace myNamespace
{
int myFunc(etc) { ... }
int main()
{
// Call your own myFunc:
myFunc();
// Call their myFunc:
::myFunc();
}
}

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.