Could somebody tell me what is the difference between
using namespace android;
....
and
namespace android {
....
}
I found that almost all .cpp files in Android source code use the second one.
Also, If I want to include some files that use the type of second one in my own project, do I need to use namespace android{...} too?
Because if I do not, compiler would report error when I call methods of included files. Or do I need to add any prefix before method call?
namespace android {
extern int i; // declare here but define somewhere
void foo ();
}
-- is used for scoping variables and functions inside a particular name. While using/calling those variables/functions, use scope resolution operator ::. e.g.
int main ()
{
android::foo();
}
There is no restriction for putting all namespace declarations in a single body instance. Multiple namespace android bodies spread across several files, is possible and also recommended sometimes. e.g.
// x.cpp
namespace android {
void somefunc_1 ();
}
// y.cpp
namespace android {
void somefunc_2 ();
}
Now, sometimes you may find using :: operator inconvenient if used frequently, which makes the names unnecessarily longer. At that time using namespace directive can be used.
This using directive can be used in function scope / namespace scope / global scope; But not allowed in class scope: Why "using namespace X;" is not allowed inside class/struct level?).
int main ()
{
using namespace android;
foo(); // ok
}
void bar ()
{
foo(); // error! 'foo' is not visible; must access as 'android::foo()'
}
BTW, Had using namespace android; declared globally (i.e. above main()), then foo() can be accessed without :: in Bar() also.
My answer is probably only helpful if you are more experienced with Java. I'm guessing since you are doing android stuff that this is the case.
The following means that you are declaring a class called MyClass in the namespace android. The qualified name of the class would be android::MyClass.
namespace android {
class MyClass {...};
}
It can be thought of similarly to the Java code:
package android;
public class MyClass {...}
The following means that you can use classes, functions etc. defined in the android namespace without having to use their qualified name (assuming they have been included).
using namespace android;
This
#include <path/to/MyClass.h>
using namespace android;
can be thought of similarly to the Java code:
import android.MyClass;
Related
I have a tough time wording this question but here's my code to start:
namespace UserInterface
{
class UiClass
{
};
}
namespace Project
{
namespace UserInterface
{
}
}
namespace Project
{
UserInterface::UiClass uiClass;
}
So that code won't work because a UserInterface is a global namespace but it's also inside Project so when I instaniate UiClass inside Project it tries to look inside Project->UserInterface instead of just UserInterface. Is there a some way to be specific that I want to use the global UserInterface and not the one inside Project or do I need to change my design?
You can force the name lookup to begin at the global scope using a leading ::.
::UserInterface::UiClass uiClass;
I'm a newbie in c++.
Why, in Eclipse (configured with MinGW) and also in other threads, I noticed is used to add a class to a namespace?
I provide an example to show you my actual doubt:
#ifndef MODEL_MANGO_HPP_
#define MODEL_MANGO_HPP_
namespace std {
class Mango {
public:
Mango();
virtual ~Mango();
};
} /* namespace std */
#endif /* MODEL_MANGO_HPP_ */
EDIT: As shown in comments, it's completely forbidden to add classes to namespace std. Quoting #owacoder,
Namespaces are never closed, so you always have the ability to add
class definitions to them. However, by the specification the std
namespace is to be considered closed.
To provide you a complete view of the context, here is the default implementation of the Mango.cpp, that Eclipse has done for me:
#include "Mango.hpp"
namespace std {
Mango::Mango() {
// TODO Auto-generated constructor stub
}
Mango::~Mango() {
// TODO Auto-generated destructor stub
}
} /* namespace std */
So my question changes into:
Why it's used "namespace std {...}" and when is a good practice to add classes to a namespace?
You have to understand the basics of what classes and namespaces are.
classes (along with structs, enums, and enum classes) are used to define user defined types in C++.
You create a class to represent a logical entity and encapsulate details, etc.
namespaces are a way to mark territories of code and qualifying unique names for variables.
if you just write a class in a file, it will be written in the "global namespace" and it is not considered good practice because you are "polluting the namespace".
instead, you should use namespaces to limit the scope where your variable names have meaning. this way, you are not exhausting the pool of sensible class and variable names quickly (how many times have you wanted to write a "Utility" class?)
namespace firstNamespace{
int x=2;
}
namespace secondNamespace{
int x=7;
}
int main ()
{
std::cout << firstNamespace::x << '\n';
std::cout << secondNamespace::x << '\n';
return 0;
}
in this case, you can see that we can "reuse" the variable name x in different Contexts by qualifying a namespace. inside the namespace blocks, we could have more declarations and definitions. including functions, classes, structs, etc.
take not that namespaces remain open and you can add to them later.
for example you can have this:
namespace firstNamespace{
int x=2;
}
namespace secondNamespace{
int x=7;
}
namespace firstNamespace{
int y=11;
}
here, we added firstNamespace::y.
More importantly, you can observe that std is a namespace provided by C++ that contains a lot of useful variables, objects like cout which is of type std::ostream, functions and classeslike std::vector, std::ostream, etc.
so to go back to your question, the reason you want to wrap your class definitions in namespaces is to not pollute the global namespace.
In C++, if I have
namespace myNamespace {
boost::whatever();
}
will the compiler look for whatever() in the boost namespace or in myNamespace::boost?
The question is not about whether it will look for boost::whatever, but where it will find boost itself.
If you have a nested namespace definition with the name boost, it will hide the global boost namespace at its point of declaration. The global boost namespace will be visible up until the point where myNamespace::boost is declared (§3.3.2/2).
[ Note: a name from an outer scope remains visible up to the point of declaration of the name that hides it. [ Example:
const int i = 2;
{ int i[i]; }
declares a block-scope array of two integers. — end example ] — end note ]
So if you're calling boost::whatever() before the nested name myNamespace::boost is created, it will look for the global boost namespace. You can qualify the name with ::boost if you always want it to find boost in the global namespace.
First of all I think you mean this:
namespace myNamespace {
using boost::whatever; // NOT boost::whatever();
}
Remember that C++ namespaces are mainly to avoid naming clashes not a design mechanism, which means when you are using a method of other namespaces in another one, you just call the original one and there is no copy in new namespace scope. So, when you call myNamespace::whatever(), compiler would choose boost::whatever() not myNamespace::boost::whatever() because there is no boost namespace declared inside myNamespace.
For more details on namespaces you could see:
http://en.cppreference.com/w/cpp/language/namespace
Assuming you want to call a function using some qualification, you may end up using a local namespace rather than a global namespace:
#include <iostream>
namespace A { void f() { std::cout << "::A::f()\n"; } }
namespace B {
namespace A { void f() { std::cout << "::B::A::f()\n"; } }
void g() { A::f(); }
void h() { ::A::f(); }
}
int main() {
B::g();
B::h();
}
If you want to make sure you are picking up a specific namespace you'll need to use absolute qualification as in ::A::f().
Is there any difference between wrapping both header and cpp file contents in a namespace or wrapping just the header contents and then doing using namespace in the cpp file?
By difference I mean any sort performance penalty or slightly different semantics that can cause problems or anything I need to be aware of.
Example:
// header
namespace X
{
class Foo
{
public:
void TheFunc();
};
}
// cpp
namespace X
{
void Foo::TheFunc()
{
return;
}
}
VS
// header
namespace X
{
class Foo
{
public:
void TheFunc();
};
}
// cpp
using namespace X;
{
void Foo::TheFunc()
{
return;
}
}
If there is no difference what is the preferred form and why?
The difference in "namespace X" to "using namespace X" is in the first one any new declarations will be under the name space while in the second one it won't.
In your example there are no new declaration - so no difference hence no preferred way.
Namespace is just a way to mangle function signature so that they will not conflict. Some prefer the first way and other prefer the second version. Both versions do not have any effect on compile time performance. Note that namespaces are just a compile time entity.
The only problem that arises with using namespace is when we have same nested namespace names (i.e) X::X::Foo. Doing that creates more confusion with or without using keyword.
There's no performance penalties, since the resulting could would be the same, but putting your Foo into namespace implicitly introduces ambiguity in case you have Foos in different namespaces. You can get your code fubar, indeed. I'd recommend avoiding using using for this purpose.
And you have a stray { after using namespace ;-)
If you're attempting to use variables from one to the other, then I'd recommend externalizing them, then initializing them in the source file like so:
// [.hh]
namespace example
{
extern int a, b, c;
}
// [.cc]
// Include your header, then init the vars:
namespace example
{
int a, b, c;
}
// Then in the function below, you can init them as what you want:
void reference
{
example::a = 0;
}
If the second one compiles as well, there should be no differences. Namespaces are processed in compile-time and should not affect the runtime actions.
But for design issues, second is horrible. Even if it compiles (not sure), it makes no sense at all.
The Foo::TheFunc() is not in the correct namespacein the VS-case. Use 'void X::Foo::TheFunc() {}' to implement the function in the correct namespace (X).
In case if you do wrap only the .h content you have to write using namespace ... in cpp file otherwise you every time working on the valid namespace. Normally you wrap both .cpp and .h files otherwise you are in risk to use objects from another namespace which may generate a lot of problems.
I think right thing to do here is to use namespace for scoping.
namespace catagory
{
enum status
{
none,
active,
paused
}
};
void func()
{
catagory::status status;
status = category::active;
}
Or you can do the following:
// asdf.h
namespace X
{
class Foo
{
public:
void TheFunc();
};
}
Then
// asdf.cpp
#include "asdf.h"
void X::Foo::TheFunc()
{
return;
}
I expected to be able to use a namespace alias in a class declaration but get a compiler syntax error.
struct MyClass
{
namespace abc = a_big_namespace;
void fn() {
abc::test();
}
};
The only way I can get it to work is to put the alias in every function.
void fn() {
namespace abc = a_big_namespace;
abc::test();
}
Additionally I would like to be able to use the alias for function parameters. I haven't found a work-around for that.
void fn(abc::testType tt) {
abc::test(tt);
}
Is there a way to do what I want?
EDIT: my solution
I found that I didn't need unnamed namespace for my particular problem and can simply do this:
namespace myspace
{
namespace abc = a_big_namespace;
struct MyClass
{
void fn(abc::testType tt) {
abc::test(tt);
}
};
}
To switch to the other library, which is what my alias namespace refers to I just change the alias. This method even allows me to have the same class in a single file twice, each time refering to a different library.
Thanks for all your help.
Namespace alias in the class definition is illegal, as specified by the language specification.
Its allowed in only in namespace scope or function scope.
You can make alias at namespace scope. But that will be make permanent alias which can be used from other files as well. But the solution is simple : you can use unnamed namespace to prevent alias (and therefore all symbols from the big namespace) from being visible from other files. This is how it can be done:
//MyFile.cpp
namespace myspace
{
namespace //this is unnamed namespace
{
namespace abc = a_big_namespace;
}
struct MyClass
{
void fn()
{
abc::test(); //don't worry, this will work!
}
};
}
//OtherFile.cpp
myspace::abc::test(); //error - that means, prevention worked.
The alias is not visible from other files. When compiling OtherFile.cpp, GCC (4.5.0) says,
'myspace::abc' has not been declared
That proves the alias abc is visible only in MyFile.cpp. Thanks to unnamed namespace.
Demo : http://www.ideone.com/2zyNI (though it doesn't demonstrate OtherFile concept. I cannot have more than one file at ideone.com)
The scope of a namespace alias is a code block.
So you can put it in any code block.
BUT, you can't put it inside a class, because that will mean it's a member of the class.
A namespace alias can't be a member.
More about namespace aliases:
What is the scope of a namespace alias in C++?
Namespaces
It works if you declare the alias outside of the struct.
You can of course also put the alias outside the class:
namespace abc = a_big_namespace;
struct MyClass {
void fn()
{ abc::test(); }
};