Difference between 'using' and 'using namespace' - c++

In the boost libraries, there are often examples of including the library like:
#pragma once
#include <boost/property_tree/ptree.hpp>
using boost::property_tree::ptree;
Throughout my program I have been importing namespaces like this:
#include "../MyClass.h"
using namespace MyClassNamespace;
Can someone please explain:
The difference between using and using namespace;
What the advantage of negating the use of using namespace in favour of using;
The differences in forward declaring using and using namespace;
Thanks

using namespace makes visible all the names of the namespace, instead stating using on a specific object of the namespace makes only that object visible.

#include <iostream>
void print(){
using std::cout;
using std::endl;
cout<<"test1"<<endl;
}
int main(){
using namespace std;
cout<<"hello"<<endl;
print();
return 0;
}
while using "using namespace std" all the elements under the scope of std are made available under scope of the function.
while using "using std::cout" we explicitly mention what element under the std is required for the function ,without importing all the elements under std.
this is my first answer in stack overflow please correct me if iam wrong!!

Related

Why do I need to do 'using namespace std' instead of 'using std::cout'?

everyone.
I have learnt that it is often desirable to write in my codes using std::cout instead of using namespace std in order to avoid namespace conflicts. In the following script I only use cout and if I write std:: cout instead of using namespace std; it does not work.
Can anyone please help me understand why? In general, when does std::cout not work and I am forced to use using namespace std?
#include <iostream>
#include <string>
using std::cout; //if writing here "using namespace std;" instead, the code does not work
class MyClass{
public:
string name;
MyClass (string n)
{
name=n;
cout<<"Hello "<<name;
}
};
int main()
{
MyClass MyObj("Mike");
return 0;
}
You need to add using std::string; along with using std::cout; to make it work as you're not only using cout from namespace std, string is also a member of namespace std which you are using in your code.
Your code works okay with:
using std::cout;
statement for cout, but the compiler must know the location of string (actually it's std::string) too which you're currently using. You must define:
using std::string;
When you enter:
using namespace std;
It calls the entire namespace called std which contains a variety of features added as C++ standard library and then you don't need to use prefix std:: for those functions/classes/variables of that namespace.

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

Is it possible to revert back to "default" global namespace?

Basically, I am working with some provided header files with the following format:
#include <iostream>
using namespace std;
class bar
{
public:
void printSomething(void)
{
cout << "This is an example." << endl;
}
}
My question is, since I can't modify the provided header, how do I strip the std namespace in my files and go back to the default global namespace? I have tried "using namespace ::;" and "using namespace ;", but the the compiler isn't happy with either of those. Any ideas on how to force a clean slate with namespaces?
You can't. That's why the using namespace clause is so evul. You could include those headers inside another namespace though:
namespace bleh {
#include "library_that_uses_evul_using_namespace.h"
}
That will pollute only the bleh namespace.
You can't get rid of a "using namespace". You can have multiple "using namespace " statements and they are additive.
However, you can wrap the malicious header into a namespace of it's own:
namespace Crap
{
#include "maliciousHeader.h"
}
That way the "using namespace std" only applies to the namespace Crap. I'd recommend putting the above code into another header which is the header that you actually include in your program.

how do define namespace and how does the compiler deal with namespace?

today i saw the C++ namespace,i suffer a problem.
what does the compiler does with namespace?
for example:
we write
#include<iostream>
using namespace std;
then the question coming ,what is the relationship between the iostream file and the namespace std? and where is the std defined ,in what file? when i use #include <iostream.h>, i know the compiler will bring the declare from iostream.h like "cout", "cin".etc to my cpp file.
can you give some suggestion? thank you in advance.
read this, it explains about namespaces
http://www.cplusplus.com/doc/tutorial/namespaces/
<iostream> contains items from the namespace std. You can look at a namespace as a grouping of methods, class definitions and variables. Using namespaces makes them easier to group by functionality.
The using directive just imports all contents of the namespace in the global namespace. But you don't have to use it:
You can either use:
using namespace std;
cout << "whatever";
or
std::cout << "whatever";
The reason for this is that the compiler doesn't know what cout is outside the namespace.
Think of it as declared like:
//file <iostream>
namespace std
{
//declaration of cout
}
//file <vector>
namespace std
{
//declaration of vector
}
the situation is like you searching for something in a library. iostream is the book and std is the page and cout is the line or paragraph.
NB: the same page can exist in multiple books
read about namespaces here.

Using a namespace twice

In c++ Is it OK to include same namespace twice?
compiler wont give any error but still will it affect in anyway
Thanks,
EDIT:
I meant
using namespace std;
// . . STUFF
using namespace std;
It depends what you mean by 'include'. Saying:
using namespace std;
...
using namespace std:
is OK. But saying:
namespace X {
...
namespace X {
would create a nested namespace called X::X, which is probably not what you wanted.
This usage is fine, if it's what your talking about:
File: foo.h
namespace tools
{
class Widget
{
...
};
}
file: bar.h
namespace tools
{
class Gizmo
{
...
};
}
Twice in the same class/file? It shouldn't be a problem, but neither should it be necessary. I'd naively assume that you should be able to refactor your code to avoid the issue.
Are you asking whether following is okay ?
using namespace std;
using namespace std;
It is okay but normally I prefer to refer the namespace element with scope resolution.
ex:
std::vector
std::sort
I'm not entirely sure what you mean. You can put multiple classes in a single namespace (that's the whole idea). Each class generally has it's own files, so yes, you can use the same namespace in multiple files.
Though not technically required, it's a good idea to have a directory structure that represents the namespace hierarchy you create.
As for for the using directive: the compiler and/or intellisense most likely gives you a warning (the C# one does), but otherwise there is no effect.