What is namespace used for, in C++?
using namespace std;
Namespace is used to prevent name conflicts.
For example:
namespace foo {
class bar {
//define it
};
}
namespace baz {
class bar {
// define it
};
}
You now have two classes name bar, that are completely different and separate thanks to the namespacing.
The "using namespace" you show is so that you don't have to specify the namespace to use classes within that namespace. ie std::string becomes string.
It's used for preventing name confilct, so you may have two classes with the same name in different namespaces.
Also it's used for categorizing your classes, if you have seen the .net framework, you will see that namespaces are used for categorizing the classes. For example, you may define a namespace for the employee classes, and a namespace for the tasks classes, and both namespaces are within a namespace for the company classes, since a namespace may contain sub namespaces.
The same namespace may exist in different files, so using it may be useful because it will make you able to directly use all the classes in the namespaces in every #included file.
That's what I remember for now.
One could ask, simple pair of curly braces {} are sufficient to resolve name conflict. Still why have a NameSpace. A quick answer as Tamer mentioned above is that with NameSpace we get the ability to open the same scope in another file.
Namespace usually is used to prevent naming conflicts.
So, One place where namespace comes into picture is
class ABC{
// Does something for me.
};
class ABC{
// Does something for you..
};
int main() {
ABC myABC;
return 0;
}
This will give a compilation error as the system will not know which class is to be considered. Here the concept of namespace comes into picture.
namespace My{
class ABC{
// Does something for me.
};
}
namespace Your{
class ABC{
// Does something for you..
};
}
using My::ABC
// We explicitly mention that My ABC is to be used.
int main() {
ABC myABC;
return 0;
}
Code will be much organized with the usage of namespaces.
Related
I remember being told that C++ classes have their own namespaces, and that the class name could be used as a namespace for scope resolution, like this:
// Example.h
class Example {
void Private();
public:
void Public();
}
and, later in a manner similar to this:
// Example.cpp
#include "Example.h"
using /*namespace*/ Example;
void Private() {}
void Public() {}
instead of:
// Example.cpp
#include "Example.h"
void Example::Private() {}
void Example::Public() {}
but I couldn't find neither an explanation nor an example of that in my books. A brief Google search was also a dead-end. Is this a real thing?
No, namespaces and classes are different.
However, namespaces and classes both introduce a scope which may be referred to using the scope resolution operator :: .
The using namespace N; declaration can only apply to namespaces. It's not possible to do something similar for a class. You can only do using Example::x; for specific names x inside Example to import them one by one.
When providing the member function body out-of-line, you must write Example::Private(), there is no alternative.
Yes, Every class has its own namespace that everything part of the class declaration belongs to.
You may employ the using directive as you indicate as explained here.
It should be noted that you cannot declare a namespace with the same name as a class as shown here:
namespace fubar
{
class snafu
{
...
};
}
// Cannot do stuff below (ERROR)
namespace fubar::snafu;
{
// this is not valid, once a class always a class
// once a namespace, always a namespace
// a class makes a namespace also but never JUST a namespace
}
Do be careful though. 'Using' too much can play real tricks on you and those who inherit your code.
I am trying to avoid including an auto-generated header (outside of my control), from inside my own headers. I therefore need to forward-declare a type Type that lives inside a namespace nm, which itself lives in the global namespace. Here is an example of MyHeader.h:
// My project defines two levels of nested namespaces
namespace foo { namespace bar {
namespace nm { struct Type; }
...
} }
Unfortunately this defines the new namespace foo::bar::nm and forward-declares the type foo::bar::nm::Type, which is not what I want. Ideally I would be able to forward-declare a type in the qualified namespace ::nm like this:
namespace foo { namespace bar {
namespace ::nm { struct Type; }
...
} }
My compiler complains I cannot use a qualified namespace here (using Intel ICC15 with C++11 settings). This forces me to put all such forward declarations at the beginning:
namespace nm { struct Type; }
namespace foo { namespace bar {
...
} }
In my case, this is inconvenient because I need to forward-declare many types and would prefer to do so alongside definitions of my own in various scattered places in my header. A workaround could be to constantly be "closing" and "re-opening" my nested namespace, which is not ideal.
Why can't I forward-declare a type in a qualified namespace ?
Because the C++ standard says so.
There is no real reason I know of. This is just not supported, probably, no one needed it and no one ever requested it. I see the benefit in your particular case, but since the forward declarations are expected to be provided in a separate file, you'd be better off by simply creating your own *_fwd.h with all the forward declarations inside it in the single namespace.
You can't because it's ambiguous. If you wrote:
extern int foo::bar;
Are you forward declaring an int named bar in namespace foo, or are you forward declaring an int named bar inside the class foo? There's no way for the compiler to know.
In a C++ implementation file, I have the option to write something like:
name::space::ClassName::someFunction(int param) {
// impl
}
instead of
namespace name {
namespace space {
ClassName::someFunction(int param) {
// impl
}
}
}
which I find very convenient and more readable. Can this somehow be used in header files, too? Something like this:
class name::space::ClassName {
// declarations
};
instead of
namespace name {
namespace space {
class ClassName {
// declarations
};
}
}
If something like this is possible, then under which conditions? I could imagine, that one would need to forward declare the namespace forward declare the classes within the namespace like this:
namespace name {
namespace space {
class ClassName;
}
}
before being able to use this:
class name::space::ClassName {
// declarations
};
in a header file.
But somehow I am not able to get this to work 1]. Is it even possible? What bothers me is, that with nested namespaces I have to indent 2 tabs until I can actually declare my class. I would like to use that space in my header files, but I don't want to omit the tabs since this would be against the "everything in curly brackets needs to be tabbed once"-rule (I don't know if there is an actual name for this. If there is, excuse my ignorance, I don't know any :S
1] The problem I faced was not related to the question. The approach actually works, but it has drawbacks (see accepted answer).
If you simply don't want the namespace braces enveloping your class definition, you can certainly achieve that by defining the namespace somewhere else with just the class declaration inside. That way, you can come up with something like this:
#ifndef MYCLASS_H
#define MYCLASS_H
namespace ClassNamespace { class MyClass; };
class ClassNamespace::MyClass
{
public:
MyClass();
~MyClass();
void a();
private:
};
#endif // MYCLASS_H
for the header and this:
#include "MyClass.h"
#include <iostream>
using namespace std;
ClassNamespace::MyClass::MyClass()
{
}
ClassNamespace::MyClass::~MyClass()
{
}
void ClassNamespace::MyClass::a()
{
cout << "hello";
}
for the implementation.
As you can see, the namespace doesn't enclose the class definition, just its declaration.
As a final note, I personally don't know why you'd want to do this or think it looks nicer to read. I'd personally much rather see the namespace encapsulate the class definition as well as the function definitions. But maybe I've been the odd one all along...
No, it doesn't work. One problem is that with
namespace name {
namespace space {
class ClassName {
// declarations
}
}
}
you can tell that it is two namespaces and one class.
However, with
class name::space::ClassName {
// declarations
}
you cannot tell if space is a namespace or a class containing a nested ClassName.
What you can do is save on your tabbing. It is possible to write
namespace name { namespace space {
class ClassName {
// declarations
}
}}
What you are seeking does not work.
One explanation is that all C++ class and struct types are implicitly associated with their own namespace (which has the same name as the class).
So, given
class name::space::ClassName
{
// declarations
};
the compiler has no way to determine whether name and space are actually namespaces or classes (at best, it must conclude that the construct is ambiguous). So the compiler needs to be explicitly told that name is a namespace, that space is a namespace within name, before being told that ClassName is a class within them.
Hence the need for
namespace name
{
namespace space
{
class ClassName;
}
}
to forward specify both namespaces and forward declare ClassName within them.
No, it is not possible. The only way to define a namespace is using the namespace keyword in combination with curly braces.
7.3.1 Namespace definition [namespace.def]
1 The grammar for a namespace-definition is
namespace-name:
original-namespace-name
namespace-alias
original-namespace-name:
identifier
namespace-definition:
named-namespace-definition
unnamed-namespace-definition
named-namespace-definition:
original-namespace-definition
extension-namespace-definition
original-namespace-definition:
inlineopt namespace identifier { namespace-body }
extension-namespace-definition:
inlineopt namespace original-namespace-name { namespace-body }
unnamed-namespace-definition:
inlineopt namespace { namespace-body }
namespace-body:
declaration-seqopt
You can define namespace's members outside the namespace, although it must have been declared inside the namespace (emphasis mine):
7.3.1.2 Namespace member definitions [namespace.memdef]
2 Members of a named namespace can also be defined outside that namespace by explicit qualification (3.4.3.2)
of the name being defined, provided that the entity being defined was already declared in the namespace
and the definition appears after the point of declaration in a namespace that encloses the declaration’s
namespace.
When you write
namespace name {
namespace space {
class ClassName;
}
}
you are not declaring a class ClassName into the namespace space, you are just providing a forward declaration.
I have a fairly large hierarchy of classes derived from a base class Entity. I would like to group these classes into a separate namespace. Currently, this looks like the following:
namespace Entity {
class Entity {
//...
};
class A : public Entity { // Instead of AEntity from before.
//...
};
class B : public Entity { // Instead of BEntity from before.
//...
};
}
int main() {
// Example usage:
Entity::Entity *entity = new Entity::B();
delete entity;
}
Having to write Entity::Entity is a bit unfortunate, so I'm wondering if this is a good idea? Adding using namespace Entity or typedef Entity::Entity Entity in the global namespace doesn't work (as that would conflict with namespace Entity). They could be added within main though...
Should I avoid naming the namespace the same as the base class within it? Or is this common practice?
Should I avoid naming the namespace the same as the base class within it?
I would. As well as the conflict you point out, it makes it awkward to refer to the namespace from within itself (sometimes necessary when names at namespace scope are hidden within a nested scope). Neither issue is particularly serious, but why make life harder than necessary?
You might call the namespace Entities, since it contains multiple entity types; and/or perhaps call the base class Base, since the namespace completes the description of what it's the base of.
I tend to used different naming conventions for namespaces (lower_case) and classes (Capitalised), but that might not suit your aesthetics or arbitrary corporate dictats.
So I have to use some members of Boost library and some from the std namespace. Right now, I have using boost::asio::ip::tcp; declared and am calling appropriate members with std:: and for example tcp::iostream server(). Is there any reason why I shouldn't add another using, namely using namespace std; and then call all the std things without std:: prefix? Can these two somehow collide or cause malfunction?
Provided that it is usually considered a bad practice to have using directives that import names from the std namespace (especially when at namespace scope and/or in a header file), because it easily leads to name clashes, consider the following program:
namespace A { void foo() { } }
namespace B { void foo() { } }
int main()
{
using namespace A;
using namespace B;
foo();
}
How should the compiler resolve the call to foo()? Well, it won't. It is ambiguous, because both A::foo() and B:foo() can now be referred to as the unqualified foo().
If there are entities with the same name in the std namespace and in the global namespace (or in any other namespace for which you have a using directive), ambiguities due to name clashes are likely to arise.
In your specific case, a using directive such as:
using namespace std;
Will be unlikely to clash with the name tcp introduced by your using declaration (notice, that a using declaration imports just one specific name and is, therefore, preferable).
Yet, it is still considered to be poor programming style and you should not do it, regardless of whether you have some other using directive or using declaration already in place.
Yes, they can. The compiler can't resolve the correct namespace in that case. The solution is to use prefixes (for example std::).
You can also put the using namespace keywords inside a function or other block, so the namespace won't be used outside it:
void foo()
{
using namespace ns;
} // ns won't be used after this
Furthermore, namespace usage can be limited to a header:
namespace ns
{
#include "ns.h"
}
However, limiting the namespace to a header is not recommended because it causes problems in many cases.
The other answers do a good job of explaining how ambiguities can be resolved when you have two or more using namespace directives, but it's worth pointing out that the situation is different if you have using declarations that pull in two or more specific names from within namespaces, like this:
namespace A { void foo() { } }
namespace B { void foo() { } }
int main()
{
using A::foo;
using B::foo;
return 0;
}
The above is always a compile error, even though neither foo is ever used. This is because using declarations, as the name suggests, actually declare a name in their enclosing scope to be an alias to a name in some other namespace. This is in contrast to a using namespace directive, which merely makes names available in their enclosing scope without actually declaring any new names.
If you really want to, it's possible to resolve the above code like this:
using fooa = A::foo;
using foob = B::foo;
But using declarations should usually be avoided anyway, because they have some other semantic surprises which can bite you in unexpected, subtle, and sometimes undetectable ways. The exception is using a base class member within a derived class, which is usually OK, though not often necessary.